Skip to content

Instantly share code, notes, and snippets.

@mgarg1
Last active April 3, 2016 20:16
Show Gist options
  • Save mgarg1/f7ebbf622475f7a810e1 to your computer and use it in GitHub Desktop.
Save mgarg1/f7ebbf622475f7a810e1 to your computer and use it in GitHub Desktop.
Important Cpp snipets
//using next permutation
while(next_permutation(vec.begin(), vec.end()) ){
// do some processing on vec
}
//using sort
std::sort (myvector.begin(), myvector.begin()+4)
std::sort (myvector.begin(), myvector.end())
//intialise vector with another array
static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
//making a pair
std::pair <int,int> foo,bar;
foo = std::make_pair (10,20);
bar = std::make_pair (10.5,'A');
//get the index of iterator
it - vec.begin()
std::distance(vec.begin(), it)
//sort with storing indices
typedef std::pair<int,int> mypair;
/*store index,value in mypair and sort with given function*/
bool comparator ( const mypair& l, const mypair& r)
{ return l.first < r.first; }
int main()
{
string st,str1;
getline(cin,st);
istringstream ss(st);
while(ss>>str1){
cout<<str1<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment