Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Learning Constants, String and Structure in C

cpp

#include <iostream>
#include <sstream>
#include <cstring>
#include <ctime>
#include <unistd.h>
#define M 3
#define w 10
using namespace std;
string nomiferramenta[]={"scatolaDiChiodi","forbici","chiaveInglese","cacciavite","martello","nastroBiadesivo"};
string nomifarmacia[]={"aspirine","oki","sciroppoPerLaGola","brioski","cerotti","disinfettante"};
string nomifruttivendolo[]={"saccoDiPatate","radicchio","cetriolo","finocchio","pomodoro","casettaDiFragole"};
string nomiNegozioDiVestiti[]={"maglietta","felpa","pantaloni","gonna","cappello","giacca"};
string nomiNegoziodianimani[]={"parrocchetto","aski","carlino","cocorito","tartarugaD'acqua","geco"};
string nomi[]={"Marco","Giorgio","Astolfo","Daria","Anna","CicciaPanza"};
string nomic[]={"Ada","Gioia","Aurora","Tonia","Eva"};
double c=(rand()%10);
struct Cosa{
    string nome="nulla";
    int pos=c=(rand()%10);
    int q=0;
    double prezzo=c=(rand()%30);
};
struct  Persona{
    string nome;
    Cosa  oggetto;
    double soldi=100.0;
};

struct Negozio{
    string nome;
    Cosa cose[M];
    int N=M;
    Persona commessa;
};

void inizializzaNegozio(Negozio &neg, string nome){
    srand(time(0));
    string nomiN[7]{"Supermecato","Negoziodianimani","NegozioDiVestiti","NegozioDiGiocattoli","Farmacia","NegozioElettronica","Ferramenta"};
    string nomi[]={"Ada","Gioia","Aurora","Tonia","Eva"};
    neg.nome=nome=nomiN[rand()%7];
    neg.commessa.nome=nomi[rand()%5];
    stringstream ss;
    for(int i=0;i<neg.N;i++) {
        ss.str(""); ss<<"oggetto_"<< i;
        neg.cose[i].nome= ss.str();
        neg.cose[i].q=w+i;
    }
}
void preleva(Negozio &neg,int pos,int quant){
    if(neg.cose[pos].q >= quant) {
        neg.cose[pos].q -=quant;
    }else {
        neg.cose[pos].q=0;
        cout << "scorte non sufficienti!!!\n";
    }
}
void deposita(Negozio &neg,int pos,int quant){
    neg.cose[pos].q += quant;
}
void stampaNegozio(Negozio neg){
        cout<<neg.nome<<" gestita da "+neg.commessa.nome+" ha "<<neg.N<<" reparti:\n";
    for(int i=0;i<neg.N;i++)
        cout<<neg.cose[i].nome<<" con la quantita\' di "<< neg.cose[i].q<<"\n";
}

void gestioneNegozio(Negozio &neg,string nome){
    inizializzaNegozio(neg, nome);
    stampaNegozio(neg);
    preleva(neg,2,5);
    deposita(neg,0,7);
    stampaNegozio(neg);
}
void stampaCliente(Persona cliente){
    std::cout << cliente.nome << " ha " << cliente.oggetto.nome;
    std::cout << " e " << cliente.soldi << " Euro\n";
}

int main() {
    srand(time (0));
   // for(int t=0;t<5;t++){
    Negozio neg;
    string nome;
    inizializzaNegozio(neg,nome);
    stampaNegozio(neg);
   
    for(int k=0;k<c;k++){
         Persona cliente;
    cliente.nome=nomic[rand()%6];
    stampaCliente(cliente);
    preleva(neg,2,1);
    cliente.oggetto = neg.cose[rand()%3];
    cliente.soldi -= neg.cose[2].prezzo;
    stampaCliente(cliente);
    }
    stampaNegozio(neg);
    cout<<"\n\n\n\n\n";
//}
   return 0;
}

Call By Address in C and C++

cpp

#include<iostream>
using namespace std;

void div(int a, int b, int *quotient, int *remainder){
   *quotient = a / b;
   *remainder = a % b;
}

main(){
   int a = 76, b = 10;
   int q, r;

   div(a, b, &q, &r);
   cout << "Quotient is: "<< q <<"\nRemainder is: "<< r <<"\n";
}

evaluation function parameters in C++

cpp

#include<iostream>
using namespace std;

void test_function(int x, int y, int z){
   cout << "The value of x: " << x << endl;
   cout << "The value of y: " << y << endl;
   cout << "The value of z: " << z << endl;
}

main(){
   int a = 10;
   test_function(a++, a++, a++);
}

C/C++ Address zero used for the null pointer

cpp

#include <stdio.h>
int main() {
   int *p= NULL;//initialize the pointer as null.
   printf("The value of pointer is %u",p);
   return 0;
}

2D vector in C++ with user defined size

cpp

#include <iostream>
#include <vector> //header file for 2D vector in C++
using namespace std;
int main() {
   vector<vector<int> > v{ { 4,5, 3, 10 }, // initializing 2D vector with values.
   { 2, 7, 11 },
   { 3, 2, 1, 12 } };
   cout<<"the 2D vector is:"<<endl;
   for (int i = 0; i < v.size(); i++) { // printing the 2D vector.
      for (int j = 0; j < v[i].size(); j++)
      cout << v[i][j] << " ";
      cout << endl;
   }
   return 0;
}

tellp() in file handling with C++

cpp

#include <iostream>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
   fstream newfile;
   newfile.open("tpoint.txt", ios::out); //open a file to perform write operation using file object
   newfile << "Tutorials Point"; //inserting data
   cout << "The present position of the pointer in the file: "
   << newfile.tellp() << endl; //position of the pointer in the file object
   newfile.close(); //close file object.
}

vector insert() function in C++ STL

cpp

#include<iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
   vector<int> v = { 50,60,70,80,90},v1;        //declaring v(with values), v1 as vector.
   vector<int>::iterator iter;                  //declaring an iterator
   iter = v.insert(v.begin(), 40);              //inserting a value in v vector before the beginning.
   iter = v.insert(v.begin(), 1, 30);           //inserting a value with its size in v vector before the beginning.
   cout << "The vector1 elements are: \n";
   for (iter = v.begin(); iter != v.end(); ++iter)
      cout << *iter << " "<<endl;             // printing the values of v vector
   v1.insert(v1.begin(), v.begin(), v.end()); //inserting all values of v in v1 vector.
   cout << "The vector2 elements are: \n";
   for (iter = v1.begin(); iter != v1.end(); ++iter)
      cout << *iter << " "<<endl;            // printing the values of v1 vector
   return 0;
}

Set vs Map in C++ STL

cpp

#include<iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
   map<char, int> m;                     //initialize a map
   map<char, int>::iterator iter;       //initializing a map as iterator
   m.insert (pair<char, int>('a', 10)); //inserting values to the map
   m.insert (pair<char, int>('b', 20));

   cout << "Elements in map:\n";
   for (iter=m.begin();iter!=m.end();iter++)
   cout << "[ " << iter->first << ", "<< iter->second << "]\n"; //printing the values of the map
   return 0;
}

C++ STL Priority Queue for Structure or Class

cpp

#include <iostream>
#include <queue>
using namespace std;
#define ROW 6
#define COL 3
struct student { //defining the student structure
   int roll,marks;
   student(int roll, int marks)
      : roll(roll), marks(marks)
   {
   }
};
struct comparemarks{ // defining the comparemarks structure
   bool operator()(student const& s1, student const& s2)
   //overloading the operators of the student structure
   {
      return s1.marks < s2.marks;
   }
};
int main()
{
   priority_queue<student, vector<student>, comparemarks> M;
   // using the priority queue.
   // We have to use this type of syntax to use the priority queue.
   int a[ROW][COL] = {{15, 50}, {16, 60},
   {18,70}, {14, 80}, {12, 90}, {20, 100}};
   for (int i = 0; i < ROW; ++i) {
      M.push(student(a[i][0], a[i][1])); //inserting variables in       the queue
   }
   cout<<"priority queue for structure ::"<<endl;
   while (!M.empty()) {
      student s = M.top();
      M.pop();
      cout << s.roll << " " << s.marks << "\n"; //printing the values
   }
   return 0;
}

C++ set lower_bound() function

cpp

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
   set<int> s;                 //Declaring an empty set container
   set<int>::iterator iter;    //Declaring a set container as iterator which will point to the lower bound value
   s.insert(7);                //inserting elements in the set container s
   s.insert(6);
   s.insert(1);
   s.insert(4);
   s.insert(2);
   s.insert(9);
   s.insert(10);
   iter = s.lower_bound(4);       //passing a key by parameter to find its lower bound
      cout <<"The lower bound of 4 is: "<< *iter << " "<<endl; //printing the lowerbound value
   iter = s.lower_bound(5);
      cout <<"The lower bound of 5 is: " <<*iter << " "<<endl;
   iter = s.lower_bound(30);
      cout <<"The lower bound of 30 is: " <<*iter << " "<<endl;

return 0;
    
}

1 2 3 4 5 6 7 ... 518 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.