Skip to content

Instantly share code, notes, and snippets.

@nobleknightt
Last active February 23, 2022 08:46
Show Gist options
  • Select an option

  • Save nobleknightt/b277ab685b77f4bfcd3b239796a11e83 to your computer and use it in GitHub Desktop.

Select an option

Save nobleknightt/b277ab685b77f4bfcd3b239796a11e83 to your computer and use it in GitHub Desktop.

Sum of elements in vector. πŸ‘‡

#include <iostream>   // std::cout
#include <vector>     // std::vector
#include <numeric>    // std::accumulate
#include <functional> // std::plus

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};
    std::cout << std::accumulate(v.begin(), v.end(), 0, std::plus<int>()); // 15
}

Product of elements in vector. πŸ‘‡

#include <iostream>   // std::cout
#include <vector>     // std::vector
#include <numeric>    // std::accumulate
#include <functional> // std::multiplies

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};
    std::cout << std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>()); // 120
}

Print whole vector using std::cout πŸ‘‡

Reference: geeksforgeeks.org

#include <iostream>   // std::cout
#include <vector>     // std::vector

template <typename T> 
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)  { 
    os << "["; 
    for (size_t i = 0; i < v.size(); ++i) { 
        os << v[i]; 
        if (i != v.size() - 1) 
            os << ", "; 
    } 
    os << "]\n"; 
    return os; 
} 

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};
    std::cout << v; // [1, 2, 3, 4, 5]
}

Print set using std::cout πŸ‘‡

Reference: geeksforgeeks.org

#include <iostream>   // std::cout
#include <set>        // std::set

template <typename T> 
std::ostream& operator<<(std::ostream& os, const std::set<T>& s)  { 
    os << "{"; 
    for (auto i : s) { 
        os << i; 
        if (i != *s.rbegin()) 
            os << ", "; 
    } 
    os << "}\n"; 
    return os; 
} 

int main() {
    std::set<int> s = {1, 2, 3, 4, 5};
    std::cout << s; // {1, 2, 3, 4, 5}
}

Print map using std::cout πŸ‘‡

Reference: geeksforgeeks.org

#include <iostream>   // std::cout
#include <map>        // std::map

template <typename T, typename S> 
std::ostream& operator<<(std::ostream& os, const std::map<T, S>& m)  {
    os << "{";
    for (auto i : m)  {
        os << i.first << ": " << i.second; 
        if (i != *m.rbegin()) 
            os << ", ";
    }
    os << "}\n";  
    return os; 
} 

int main() {
    std::map<int, int> m = {{1, 1}, {2, 4}, {3, 9}};
    std::cout << m; // {1: 1, 2: 4, 3: 9}
}

Fast Input-Output πŸ‘‡

Reference: stackoverflow.com

#include <iostream>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    
    // your code goes here
    
    return 0;
}

ONLINE_JUDGE πŸ‘‡

#include <iostream>
using namespace std;

int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin); 
        freopen("output.txt", "w", stdout); 
    #endif
    
    // your code goes here
    
    return 0;
}

ONLINE_JUDGE (Modern C++) πŸ‘‡

Reference: stackoverflow.com

#include <iostream>
using namespace std;

int main() {   
    #ifndef ONLINE_JUDGE
        ifstream input("input.txt"); cin.rdbuf(input.rdbuf());
        ofstream output("output.txt"); cout.rdbuf(output.rdbuf());
    #endif 
    
    // your code goes here
    
    return 0;
}

split() implementation using getline() πŸ‘‡

Reference: stackoverflow.com

#include <iostream> // std::cout
#include <sstream>  // std::sstream
#include <string>   // std::string
#include <vector>   // std::vector

std::vector<std::string> split(const std::string& s, const char& delim) {
    std::vector<std::string> list;
    std::stringstream ss(s);
    std::string item;

    while (getline (ss, item, delim)) {
        if (item.empty())
    	    continue;
        list.emplace_back(item);
    }

    return list;
}

int main() {
    std::vector<std::string> list = split("a--b-c-d", '-'); // [a, b, c, d]
    return 0;
}

Print an integer in binary, octal and hexadecimal format πŸ‘‡

Reference: stackoverflow.com

#include <iostream>
#include <bitset>  // std::bitset
#include <iomanip> // std::dec, std::oct, std::hex

int main() {
    std::cout << std::dec << 42 << std::endl;     // 42
    std::cout << std::bitset<8>(42) << std::endl; // 00101010
    std::cout << std::oct << 42 << std::endl;     // 52
    std::cout << std::hex << 42 << std::endl;     // 2a
    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment