Last active
November 1, 2015 13:53
-
-
Save ilebedie/d6ec6140378119396de8 to your computer and use it in GitHub Desktop.
stl_howto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <numeric> | |
#include <sstream> | |
#include <algorithm> | |
#include <fstream> | |
#include <iterator> | |
#include <map> | |
#include <set> | |
#include <bitset> | |
#include <thread> | |
#include <future> | |
#include <cstring> //strerror() | |
#include <memory> | |
using namespace std; | |
//void reading_from_file1() | |
//{ | |
// ifstream ff("/home/ilebedie/test.txt"); | |
// int t; | |
// ff>>t; | |
// cout<<t; | |
// string line; | |
// vector<int> vec; | |
// while(getline(ff, line)) | |
// { | |
// istringstream ss(line); | |
// int val; | |
// while (ss>>val) | |
// { | |
// vec.push_back(val); | |
// } | |
// cout<<line<<'\n'; | |
// } | |
// cout<<"vector vals====\n"; | |
// for(int v: vec) | |
// { | |
// cout<<v<<" "; | |
// } | |
// cout<<"\n"; | |
// return 0; | |
//} | |
/////////////////////////////////////// | |
//for print | |
//template<typename T> | |
//void print(T el) | |
//{ | |
// cout<<el<<" "; | |
//} | |
void print(){ cout<<"\n"; } | |
template<typename T, typename ...Tail> | |
void print(T head, Tail... tail) | |
{ | |
cout<<head<<" "; | |
print(tail...); | |
} | |
/////////////////////////////////////// | |
template<typename C> | |
void printc(C c) | |
{ | |
for(auto& el: c) | |
cout<<el<<" "; | |
cout<<'\n'; | |
} | |
template<typename K, typename V> | |
void printc(const std::multimap<K,V>& m) | |
{ | |
for(auto& el: m) | |
cout<<"key:"<<el.first<<" value:"<< el.second<<'\n'; | |
} | |
template<typename K, typename V> | |
void printc(const std::map<K,V>& m) | |
{ | |
for(auto& el: m) | |
cout<<"key:"<<el.first<<" value:"<< el.second<<'\n'; | |
} | |
void reading_from_file2() | |
{ | |
ifstream ff("/home/ilebedie/test.txt"); | |
int t; | |
ff>>t; | |
cout<<t; | |
//vector<int> vec; | |
//copy(istream_iterator<int>(ff), | |
// istream_iterator<int>(), | |
// back_inserter(vec)); | |
vector<int> aa{istream_iterator<int>{ff},istream_iterator<int>{}}; | |
printc(aa); | |
} | |
//void string_manip() | |
//{ | |
// string filename("asdf.asdf"); | |
// string::size_type idx = filename.find(’.’); | |
// if(idx!= string::npos) | |
// { | |
// string basename = filename.substr(0, idx); | |
// string extname = filename.substr(idx+1); | |
// string tmpname; | |
// tmpname.replace (idx+1, extname.size(), "xxx"); | |
// tmpname.replace (idx+1, string::npos, "xxx"); | |
// string delims = "asdf"; | |
// int start_idx = 3; | |
// int begidx = tmpname.find_first_not_of(delims,start_idx); | |
// basename.append(extname,2,string::npos) | |
// string s = "i18n"; | |
// s.replace(1,2,"nternationalizatio"); | |
// s.erase(7,5); | |
// string s2 = "aaa"; | |
// string s3 = move(s) + move(s2); | |
// | |
//transform (s.cbegin(), s.cend(), // source | |
// s.begin(), | |
// // destination | |
// [] (char c) { | |
// // operation | |
// return tolower(c); | |
// }); | |
// don’t skip leading whitespaces | |
//cin.unsetf (ios::skipws); | |
// read all characters while compressing whitespaces | |
//const locale& loc(cin.getloc()); | |
//// locale | |
//unique_copy(istream_iterator<char>(cin), // beginning of source | |
//istream_iterator<char>(), | |
//// end of source | |
//back_inserter(input), | |
//// destination | |
//[=] (char c1, char c2) { | |
//// criterion for adj. duplicates | |
//return isspace(c1,loc) && isspace(c2,loc); | |
//}); | |
//} | |
void use_unique() | |
{ | |
std::vector<int> v{1,2,3,1,2,3,3,4,5,4,5,6,7}; | |
std::sort(v.begin(), v.end()); | |
auto last = std::unique(v.begin(), v.end()); | |
v.erase(last, v.end()); | |
for(int x: v) cout<<x<<" "; | |
cout<<"\n"; | |
v.assign({1, 2, 2, 2, 3, 3, 2, 2, 1}); | |
last = std::unique(v.begin(), v.end()); // 1 2 3 2 1 3 2 2 1 | |
v.erase(last, v.end()); | |
for(int x: v) cout<<x<<" "; | |
cout<<"\n"; | |
} | |
void reverse_str() | |
{ | |
string tt("asdf qwer"); | |
reverse(tt.begin(), tt.end()); | |
char tt1[] = "zcv bnm"; | |
string tt1s(tt1); | |
reverse(begin(tt1s), end(tt1s)); | |
cout<<tt<<'\n'; | |
cout<<tt1s.c_str()<<'\n'; | |
} | |
#define check_io(x) \ | |
try \ | |
{\ | |
(x);\ | |
}\ | |
catch(ios_base::failure e)\ | |
{\ | |
std::cout << strerror(errno) << std::endl;\ | |
cout<<"[io_check] " <<__FILE__<<":"<<__LINE__<< " - " <<__FUNCTION__;\ | |
throw;\ | |
}\ | |
void common_io_operations_does_not_work() | |
{ | |
//Write output bundle index | |
string filename; | |
std::ofstream ofile; | |
char * buffer_ptr = nullptr; | |
int buffer_size = 0; | |
std::unique_ptr<std::ofstream> writer(new std::ofstream()); | |
ofile.exceptions ( std::ofstream::failbit | std::ofstream::badbit ); | |
check_io( ofile.open( filename )); | |
//check_io( ofile << to_xml(output_index ) ); | |
check_io( writer->write(reinterpret_cast<char *>(buffer_ptr), buffer_size)); | |
char * buff = nullptr; | |
int len = 0; | |
string media_path; | |
int frame_offset = 0; | |
std::unique_ptr<std::ifstream> reader(new std::ifstream()); | |
check_io(reader->open(media_path, ios::binary)); | |
reader->exceptions(ifstream::failbit | ifstream::badbit); | |
//Read sample | |
check_io( reader->seekg(frame_offset, reader->beg)); | |
check_io( reader->read((char*)(buff), len)); | |
} | |
template<class T> | |
void print_container(const T& cont) | |
{ | |
for(auto& el: cont) | |
cout<<el<<' '; | |
cout<<'\n'; | |
} | |
void vector_manip() | |
{ | |
vector<int> vec = {1,2,3,3,3,4,5,6}; | |
auto lasto = prev(vec.end()); | |
vec.erase(remove(vec.begin(), | |
vec.end(), | |
3), vec.end()); | |
print_container(vec); | |
//trying to invalidate container | |
//really hard to show | |
vector<int> vec2 = {7,7,7,7,7,7}; | |
copy(vec2.begin(), vec2.end(), vec.begin()+2 ); | |
print_container(vec); | |
print("lasto:", *lasto); | |
} | |
void algos() | |
{ | |
// First 10 even numbers: | |
vector<int> numbers = {2,2,2,2, 2,2,2,2, 2,2}; | |
print("first 10 numbers:"); | |
partial_sum(numbers.begin(), numbers.end(), ostream_iterator<int>(cout, " ")); | |
print("\n First 10 powers of 2"); | |
partial_sum(numbers.begin(), numbers.end(), ostream_iterator<int>(cout, " "), multiplies<int>()); | |
} | |
struct oper | |
{ | |
bool operator()(int i, int j){return i>j;}; | |
}; | |
void map_manip() | |
{ | |
map<int, string> dict = {{3,"bubbu"}, {1,"asdf"}, {2,"qewrty"}, {7,"jkl;"} }; | |
printc(dict); | |
//rename key | |
dict[33] = dict[3]; | |
dict.erase(3); | |
printc(dict); | |
//upper and lower bound | |
print(dict.count(33)); | |
auto r = dict.lower_bound(3); | |
print(r->second, " "); | |
//replace the content | |
dict = { {1,"10"}, {2,"20"},{3,"30"}, {4,"40"} }; | |
//adding more values | |
dict.insert( {{5,"50"}, {6,"60"}} ); | |
//removing element with certain value | |
for (auto pos = dict.begin(); pos!= dict.end();) | |
{ | |
if(pos->second == "20") | |
pos = dict.erase(pos); | |
else | |
++pos; | |
} | |
dict[7] = "70"; | |
print(dict[8], " "); | |
try | |
{ | |
dict.at(9); | |
} | |
catch(out_of_range& e) | |
{ | |
cout<<"caugt exception\n"; | |
} | |
printc(dict); | |
} | |
void set_manip() | |
{ | |
set<int> unique_values{ {6,4,5,3,1,2,3,2,1,1,2,1,1} }; | |
printc(unique_values); | |
} | |
void bitset_manip() | |
{ | |
enum color{red,green,blue,yellow,numcolors}; | |
bitset<numcolors> usedColors; | |
usedColors.set(red); | |
usedColors.set(green); | |
print("Any", usedColors.any()); | |
print("How many set:", usedColors.count()); | |
usedColors.set(blue); | |
usedColors.set(yellow); | |
print("All",usedColors.all()); | |
print("Now flip", usedColors.flip()); | |
print("267 as binary short: ", bitset<numeric_limits<unsigned short>::digits>(267) ); | |
print("colors as number:", usedColors.to_ullong()); | |
usedColors.set(red); | |
print("shifted colors: ", usedColors<<1); | |
} | |
void vector_bool_manip() | |
{ | |
vector<bool> vb = {true, false, false, true}; | |
for(auto it = vb.begin(); it!= vb.end(); it++) | |
cout<<*it; | |
vb[2] = true; | |
cout<< "\n"; | |
for(auto it = vb.begin(); it!= vb.end(); it++) | |
cout<<*it; | |
cout<< "\n"; | |
vb.resize(25, true); | |
for(auto it = vb.begin(); it!= vb.end(); it++) | |
cout<<*it; | |
cout<<'\n'; | |
vb[1].flip(); | |
vb.flip(); | |
for(auto it = vb.begin(); it!= vb.end(); it++) | |
cout<<*it; | |
vector<vector<bool>> rrr {{true, true}, {false, false}}; | |
print("value of vec<bool>: ",rrr[0][1] ); | |
} | |
double acc(double *beg, double *end, double init) | |
{ | |
return accumulate(beg,end,init); | |
} | |
void futures_and_promises() | |
{ | |
using task_t = double(double*, double*, double); | |
packaged_task<task_t> pt1(acc); | |
future<double> f1{pt1.get_future()}; | |
} | |
int main2() | |
{ | |
// futures_and_promises(); | |
//algos(); | |
//int k = max(3,2); | |
//vector_bool_manip(); | |
//bitset_manip(); | |
//set_manip(); | |
//map_manip(); | |
//vector_manip(); | |
//print("so what", 3, 234); | |
//use_unique(); | |
//reverse_str(); | |
//reading_from_file2(); | |
//int val = 0b11111111; | |
//print("output mask:", val); | |
return 0; | |
} | |
//interesting examples | |
// vector<int> aaa = {3,2,1,5,1}; | |
// auto maxpos = max_element(aaa.begin(), aaa.end()); | |
// cout<<"minpos:"<<*maxpos<<"\n"; | |
// string text = | |
// "Liebes gutes\n" | |
// "Kleines Schwein\n" | |
// "lass mich doch\n" | |
// "zu dir hienein!"; | |
// istringstream iss(text); | |
// for (int i = 0; i<4; i++) | |
// { | |
// string s; | |
// getline(iss, s); | |
// cout<<"==="<<s<<"\n"; | |
// } | |
// int age; | |
// std::cout<<"Enter your age: "; | |
// std::cin>>age; | |
// std::cin.ignore(); | |
// std::cout<<"You entered "<<age<<std::endl; | |
// std::cin.get(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment