Tutorialspoint

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;
}

Compile and Execute C++0x Onlinej

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;
    
}

Compile and Execute C++0x Onlinej

cpp

#include <iostream>
using namespace std;

class B { 
public: 
   virtual ~B() = 0; 		// Pure virtual destructor 
};

B::~B() { 
  	std::cout << "Pure virtual destructor is called"; 
} 

class D : public B { 
public: ~D() { 
      cout << "~D() is executed"<<endl; 
   } 
}; 

int main() { 
	B *bptr = new D(); 
	delete bptr; 
	return 0; 
}

Compile and Execute C++0x Onlinej

cpp

#include<iostream>
using namespace std;
int CountInversionArray(int a[], int n) {
    int i, j, c = 0;
    for(i = 0; i < n; i++)
    {
    for(j = i+1; j < n; j++)
    if(a[i] > a[j])
    c++;
    }
    return c;
}
int main() {
    int n, i;
    cout<<"\nEnter the number of elements: ";
    cin>>n;
    int a[n];
    for(i = 0; i < n; i++)
    {
        cout<<"\nEnter element "<<i+1<<": ";
        cin>>a[i];
    }
    cout<<"\nThe number of inversion in the array: "<<CountInversionArray(a, n);
    return 0;
}

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

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