Vector in C++

Vector Codes:

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

int main(){

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);

for(int i=0; i<v.size(); i++){
    cout<<v[i]<<endl;
}


return 0;
}

Vector Copy:

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

int main(){

vector<int> v,t;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
t=v;
for(int i=0; i<t.size(); i++){
    cout<<t[i]<<endl;
}


return 0;
}

String:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdio>
using namespace std;

int main(){
string a,b,c;
a="this is a string";
b=a;
c=a+b;
cout<<c<<endl;
//printf("%s\n",c.c_str());
for(int i=0; i<c.size(); i++){
cout<<c[i]<<endl;
}
return 0;
}

Stack:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdio>
#include<stack>
using namespace std;

int main(){
stack<int> st;

st.push(100);
st.push(101);
st.push(102);

while(!st.empty()){
    cout<<st.top()<<endl;
    st.pop();
}

return 0;
}

Queue :

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;

int main(){
queue<int> q;
q.push(100);
q.push(101);
q.push(102);

while(!q.empty()){
cout<<q.front()<<endl;
q.pop();
}
return 0;
}

Priority Queue:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;

int main(){
priority_queue<int> q;
q.push(100700);
q.push(101);
q.push(1029990);

while(!q.empty()){
cout<<q.top()<<endl;
q.pop();
}
return 0;
}

Iterator:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdio>
#include<stack>
#include<iterator>
#include<queue>
using namespace std;

int main(){

    vector<int> v;
    vector<int>::iterator i;

    v.push_back(1);
    v.push_back(2);
    v.push_back(3);


    for(i=v.begin(); i<v.end(); i++){

        printf("%d\n", *i);

    }

return 0;
}

Set:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdio>
#include<stack>
#include<iterator>
#include<queue>
#include<set>
using namespace std;

int main(){

   set<int> s;
   s.insert(10);
   s.insert(5);
   s.insert(9);

   set<int>::iterator it;
   for(it=s.begin(); it!=s.end(); it++){
        cout<<*it<<endl;

   }
   
return 0;
}

Map:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdio>
#include<stack>
#include<iterator>
#include<queue>
#include<set>
#include<map>
using namespace std;

int main(){

map<string, int> m;
string goru;

while(cin>>goru){
    if(goru=="moro")
        break;
    m[goru]++;
    cout<<goru<<" ase "<<m[goru]<<" ta :D"<<endl;
}

return 0;
}

String Stream:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdio>
#include<stack>
#include<iterator>
#include<queue>
#include<set>
#include<map>
#include<sstream>
#include<algorithm>
using namespace std;

int main(){

char line[1000];
int num;
while(gets(line)){
stringstream ss(line);
    vector<int> v;
    while(ss>>num){
        v.push_back(num);
        sort(v.begin(), v.end());
    }
}


return 0;
}

Good websites for learning by example:
http://www.sanfoundry.com/cpp-program-implement-vector-stl/

http://www.progkriya.org/gyan/stl.html

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *