#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
}#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
}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]
}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}
}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}
}Reference: stackoverflow.com
#include <iostream>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
// your code goes here
return 0;
}#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;
}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;
}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;
}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;
}