Skip to content

Instantly share code, notes, and snippets.

View steveobbayi's full-sized avatar

Steve Obbayi steveobbayi

View GitHub Profile
vector<int> v1 = {3,4};
vector<int> v2 = {1,2,5,6};
// Get the position of "5" in the vector and store it in i
vector<int>::iterator i = find(v2.begin(), v2.end(), 5);
//Now we use the insert iterator to add all elements in
//v1 to v2 at the position of i
insert_iterator<vector<int>> ins_i(v2, i);
copy(v1.begin(), v1.end(), ins_i);
vector<string> v1;
// copy everything from std input and insert it at the back of v1
copy(istream_iterator<string> cin, istream_iterator<string>(), back_inserter(v1));
// the contents of v1 can be copied to std output separated by a space
copy(v1.begin(), v1.end(), ostream_iterator<string>(cout, " "));
// this simply does the above in a single statement
copy(istream_iterator<string>(cin), istream_iterator<string>(), ostream_iterator<string>(cout, " "));
vector<int> v = {1,2,3,4};
reverse_iterator<vector<int>::iterator> ri;
for(ri = v.rbegin(); ri != v.rend(); ri++)
cout << *ri<< endl;
// The above prints out 4,3,2,1
vector<string> v{"our", "original", "content"};
vector<string>::iterator i;
string concat = accumulate(move_iterator<i>(v.begin()), move_iterator<i>(v.end()), string());
//Concatenated as string: "ouroriginalcontent"
//New contents of the vector is "" "" ""
vector<int> v = {3, 5, 2, 7, 1, 8, 4, 9};
vector<int>::iterator i = min_element(v.begin(), v.end());
// i is one
// remember end() is always one element past the last element.
// See how it works here
sort(v.begin(), i);
// the above returns {2,3,5,7,1,8,4,9}
//notice nothing after 7 is sorted because i which
// is 1 element after the last is not counted
fn main() {
println!("hello, world");
}
unordered_set<string> uset = {"three","two","three"};
unordered_set<string>::cons_iterator it = uset.find("three");
if(it != uset.end())
cout << *it << endl;
uset.insert("four");
//More on hash tables on the unordered set
cout << "The load factor is " << uset.load_factor() << endl;
unordered_map<char, string> month = {{'J',"January"},{'F',"February"}},{'M',"March"}};
cout << month['J'] << endl; //Just like array, no range check (unsafe)
cout << month.at('J') << endl; //we have a range check (safe)
//inserting a new month
month['A'] = "April";
month.insert(make_pair('S',"September")); // Insert as a key/value pair
month.insert(make_pair('M',"March")); //Fail seeing duplicates not allowed but
month['M'] = "May"; //Allowed so now M has been modified (not safe Why?)
void changeMonth(const unordered_map<char, <string>> m){
m['J'] = "June"; // compile error the function is constant
cout << m['J'] << endl; //compile error as above. The implementation attempts to alter
//To read from the map you must use the built in algorithm like so
auto it = m.find('J');
if(if != m.end()) //important check as usual
cout << *it << endl;
}
changeMonth(month);
set<int> s;
s.insert(2); //s: {2};
s.insert(1); //s: {1,3} <~ Note the auto sorting
s.insert(3); //s: {1,2,3} <~ sorting retained
set<int>::iterator i;
i = s.find(2); // Points to the second element {2}
//insert returns a pair
pair<set<int>::iterator, bool> p;