Skip to content

Instantly share code, notes, and snippets.

@dmitru
Created September 26, 2015 21:08
Show Gist options
  • Save dmitru/f532f30002283768bf46 to your computer and use it in GitHub Desktop.
Save dmitru/f532f30002283768bf46 to your computer and use it in GitHub Desktop.
An example showing usage of some std containers in C++11
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
int main()
{
std::vector<int> a = {1, 2, 3, 4, 5};
a.push_back(42);
a.erase(a.begin() + 3);
for (auto &el : a) {
std::cout << el << std::endl;
}
// Initializing a map with an initialization list
std::map<int, std::vector<std::string>> map({
{1, { "Ignoring", "The", "Voices" } },
{2, { "In", "My", "Head" } }
});
map[666] = {"Number", "of", "the", "beast"};
for (auto i = map.begin(); i != map.end(); i++)
{
std::cout << i->first << " :";
for (auto &el : i->second)
std::cout << " " << el;
std::cout << std::endl;
}
std::set<std::string> s = {};
s.insert("abc");
s.insert("def");
if (s.find("abc") != s.end()) {
std::cout << "s contains 'abc'" << std::endl;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment