Wednesday 5 August 2015

Quick Syntax Reminder Basic Data Structure C++

Maps:

maps<int,int> m;
m[key1] = value1;
m[key2] = value2;

How to iterate
for(map<int,int>::iterator it = m.begin() ; it != m.end(); it++){
                cout<<"Key is "<<it->first<<" ";
cout<<"Value is "<<it->second<<endl;
}

List:

list<int> *adj = new list<int>[size];
adj[index1].push_back(value);

 for(list<int>::iterator i = g->adj[u].begin(); i!=g->adj[u].end(); ++i){
cout<<"->"<<*i;
}

Overload < operator for Sort() Function 

class freq
{
public:
int data;
int count;
};


inline bool operator<(const freq& a, const freq& b)
{
return a.count <= b.count;
}
 

sort(arr,arr+n);

No comments:

Post a Comment