mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 01:51:38 +00:00
[c++/en] container -> vector (#2838)
* container -> vector fixed errors: - "vector_name" and "Vector_name" (different case) would have resulted in a compile time error, now: "my_vector" enhancements: - typedef for consistency - two push_backs to show its purpose - both iteration types now have a working execution block (both output the vector's content) - the first "classic loop" now also shows the operator [], which therefor is removed from below - include and for with a white spaces for readability * removed the typedef the `typedef` was used to show that we will be using `string` as our base for all operations, but we are free to use any other type; of course it is technically not needed and might look like a redundancy. the two `cin` also look redundant, so I changed this into one `cin` and two `push_back`s
This commit is contained in:
parent
a148661c74
commit
cd379d9e9e
@ -1000,24 +1000,24 @@ cout << get<5>(concatenated_tuple) << "\n"; // prints: 'A'
|
|||||||
|
|
||||||
// Vector (Dynamic array)
|
// Vector (Dynamic array)
|
||||||
// Allow us to Define the Array or list of objects at run time
|
// Allow us to Define the Array or list of objects at run time
|
||||||
#include<vector>
|
#include <vector>
|
||||||
vector<Data_Type> Vector_name; // used to initialize the vector
|
string val;
|
||||||
|
vector<string> my_vector; // initialize the vector
|
||||||
cin >> val;
|
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:
|
// To iterate through a vector we have 2 choices:
|
||||||
// Normal looping
|
// Either classic looping (iterating through the vector from index 0 to its last index):
|
||||||
for(int i=0; i<Vector_name.size(); i++)
|
for (int i = 0; i < my_vector.size(); i++) {
|
||||||
// It will iterate through the vector from index '0' till last index
|
cout << my_vector[i] << endl; // for accessing a vector's element we can use the operator []
|
||||||
|
}
|
||||||
// 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
|
|
||||||
|
|
||||||
|
// 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
|
// Set
|
||||||
// Sets are containers that store unique elements following a specific order.
|
// Sets are containers that store unique elements following a specific order.
|
||||||
|
Loading…
Reference in New Issue
Block a user