12558 - I2P(II) Chen_cpp_cheatsheet   

Description

I/O optimization

If you encounter a TLE, you may try to use this optimization for C++ I/O functions:
std::ios_base::sync_with_stdio(false);
cin.tie(0);
And replace all std::endl with '\n'.
Do note that you must not use any of C I/O functions such as scanf, printf, fgets if you use this optimization.

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

<queue>

#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
int main() {
    string s;
    queue<string> q;
    while (cin >> s) {
        if (s == "Push") {
            cin >> s;
            q.push(s);
        }
        else if (s == "Pop") {
            if (q.size()>0)
                q.pop();
        }
        else if (s == "Front") {
            if (q.size() == 0)
                cout << "empty\n";
            else
                cout << q.front() << "\n";
        }
    }
}

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

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

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

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

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

<string>

#include <iostream>
#include <string>
int main() {
    std::string str1, str2, out;
    while (std::cin >> str1 >> str2) {
        out.clear();
        for(auto t : str1) out.push_back(t);
        std::cout << out << '\n';
        out += str2;
        std::cout << out << '\n';
        out.pop_back();
        std::cout << out << '\n';
        for(size_t i = 0; i < out.size(); i++)
            std::cout << out[i];
        std::cout << '\n';
    }
}

Miscellaneous

For more standard library or C++ reference, please refer to this file and rename the downloaded file(12534.cpp) to reference.zip, then unzip it. There's a readme inside the zip.

If you have difficulty renaming file, try the following command on terminal (Windows)

move 12534.cpp reference.zip

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss