11072 - I2P(II)2020 final exam Cheatsheet   

Description

1. How to make cin faster?

std::ios_base::sync_with_stdio(false);

use '\n' instead of endl

2. An example of using <vector>

#include <iostream>

#include <vector>

#include <algorithm> // sort

int main ()

{

     std::vector<int> V;

     int x;

     while (std::cin>>x) {

          V.push_back(x);

     }

     for (auto t : V) std::cout<<" "<< t;

     std::cout << "\n";

     std::sort(V.begin(), V.end());

     for (auto t : V) std::cout<<" "<< t;

     std::cout << "\n";

}

 

3. An example of using <stringstream>

#include <string>
#include <iostream>
#include <sstream> // std::stringstream
int main () {

     std::stringstream ss;
     ss << "Hello " << 123;
     std::string s = ss.str();
     std::cout << s << '\n';
}

 

4. An example of using <set>
#include <iostream>
#include <set>
int main()
{
     std::set<int> S = {1, 2, 3, 4};
     auto search = S.find(2);
     if(search != S.end()) {
          std::cout << (*search) << '\n';
     }
     else {
          std::cout << "Not found\n";
     }

}

 

5. An example of using <stack>

#include <stack>
#include <iostream>
int main()
{
     std::stack<int> s;
     s.push( 3 ); s.push( 6 );
     s.push( 18 );
     std::cout<<s.size()<<" elements\n";
     std::cout<<"Top: "<< s.top()<< "\n";
     s.pop();
     std::cout<<s.size()<<" elements\n";
}

6. An example of getline()

#include <string>
#include <iostream>
int main()
{
     std::string name;
     std::cout << "What is your name? ";
     std::getline(std::cin, name);
     std::cout << "Hello "<< name<< "\n";
}

 

7. An example of using <map>

#include <iostream>
#include <string>
#include <map>
#include <utility> // make_pair
int main()
{
     std::map<std::string, int> ID;
     ID.insert(std::make_pair("Tom", 123));
     std::cout << ID["Tom"] << "\n";
}

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss