Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Using c++11 range-base for loop
//Title of this code // // range-based for // #include <iostream> #include <string> #include <vector> #include <algorithm> #include <iterator> using namespace std; template<class T> ostream& operator<<(ostream& cout, const vector<T>& v){ for(T i : v){ cout << i << " "; } return cout; } void msgToUser(){ string userName("", 125); cout << "User's name:\t"; getline(cin, userName, '\n'); cout << userName << endl; cout << "Good day " << userName << endl; }; template<class T> class InitData{ private: T val; //T res; public: InitData(){}; InitData(const T& init): val(init){}; ~InitData(){}; T operator()(){ return val++; }; }; int main(){ msgToUser(); double d{1.5}; cout << "d = " << d << endl; vector<double>v1{1,2,3,4.5,5.25}; vector<int>iv1{1,2,6,4,2,8,9,10}; vector<double>v2 = {5,10,20,40,80}; vector<char>UC(26); vector<char>LC(26); cout << endl; cout << "v1 size = " << v1.size() << endl; cout << "v1 content:\t" << v1 << endl; cout << "\niv1 size = " << iv1.size() << endl; cout << "iv1 contents:\t" << iv1 << endl; cout << "\nv2 contents:\t" << v2 << endl; auto d1 = 2.25; auto lc = 'a'; auto uc = 'A'; auto Int = {4095, 8025, 16}; int IntSize = sizeof(Int)/sizeof(int); vector<int>iv2(Int); cout << "iv2:\t" << iv2 << endl; vector<double>v3(10); InitData<double>gen(1.25); generate(v3.begin(), v3.end(), gen); cout << "v3:\t" << v3 << endl; vector<int>v4(10); InitData<int>iGen(1); generate(v4.begin(), v4.end(), iGen); cout << "v4:\t" << v4 << endl; InitData<char>ucgen(uc); InitData<char>lcgen(lc); generate(UC.begin(), UC.end(), ucgen); generate(LC.begin(), LC.end(), lcgen); cout << "Upper case set:\t" << UC << endl; cout << "Lower case set:\t" << LC << endl; return 0; }
run
|
edit
|
history
|
help
0
ThreadContext
Dulce enredo p1
Maximum product subarray
Nobita's candies problem
NamespaceId
SD
Size of data type test
temp_new
new delete malloc free
Making pyramid using nested loop 2/2