diff options
-rw-r--r-- | c++.html.markdown | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/c++.html.markdown b/c++.html.markdown index 23013410..8d1c7a26 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -1000,24 +1000,24 @@ cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A' // Vector (Dynamic array) // Allow us to Define the Array or list of objects at run time -#include<vector> -vector<Data_Type> Vector_name; // used to initialize the vector +#include <vector> +string val; +vector<string> my_vector; // initialize the vector cin >> val; -Vector_name.push_back(val); // will push the value of variable into array +my_vector.push_back(val); // will push the value of 'val' into vector ("array") my_vector +my_vector.push_back(val); // will push the value into the vector again (now having two elements) -// To iterate through vector, we have 2 choices: -// Normal looping -for(int i=0; i<Vector_name.size(); i++) -// It will iterate through the vector from index '0' till last index - -// Iterator -vector<Data_Type>::iterator it; // initialize the iterator for vector -for(it=vector_name.begin(); it!=vector_name.end();++it) - -// For accessing the element of the vector -// Operator [] -var = vector_name[index]; // Will assign value at that index to var +// To iterate through a vector we have 2 choices: +// Either classic looping (iterating through the vector from index 0 to its last index): +for (int i = 0; i < my_vector.size(); i++) { + cout << my_vector[i] << endl; // for accessing a vector's element we can use the operator [] +} +// or using an iterator: +vector<string>::iterator it; // initialize the iterator for vector +for (it = my_vector.begin(); it != my_vector.end(); ++it) { + cout << *it << endl; +} // Set // Sets are containers that store unique elements following a specific order. |