There are several different ways to create and initialize the contents of a vector.
1. Create an empty vector and push back the data:
1 2 3 4 5 6 7
|
std::vector<std::string> nameCountries;
nameCountries.push_back("China");
nameCountries.push_back("India");
nameCountries.push_back("Russia");
nameCountries.push_back("UK");
nameCountries.push_back("USA");
|
2. Create a sized vector and modify the elements:
1 2 3 4 5 6 7 8 9
|
int NUMBER_OF_COUNTRIES = 5;
std::vector<std::string> nameCountries(NUMBER_OF_COUNTRIES);
nameCountries[0] = "China";
nameCountries[1] = "India";
nameCountries[2] = "Russia";
nameCountries[3] = "UK";
nameCountries[4] = "USA";
|
(Using
operator[]
is more common than using the vector's
at()
method.)
3. Since C++11 a vector can be created and initialized with an initializer list:
std::vector<std::string> nameCountries { "China", "India", "Russia", "UK", "USA" };
Notice that 1 & 3 don't need to specify a size to create. 1 increases the size with each push_back(). 3 is sized from the number of arguments in the initializer list.
2 does create a sized vector, but unlike a C style array doesn't need a const variable value.
Now that we've created our vector, totally forget the variable used to create the size. A vector internally stores the number of elements it contains. Use that for your loops.
There are several ways to loop through a vector.
1. Use the vector's
size()
method:
1 2 3 4
|
for (size_t loop = 0; loop < nameCountries.size(); loop++)
{
std::cout << nameCountries[loop] << '\n';
}
|
China
India
Russia
UK
USA |
2. Use vector's iterators:
1 2 3 4
|
for (auto itr = nameCountries.begin(); itr != nameCountries.end(); itr++)
{
std::cout << *itr << '\n';
}
|
(This loop will allow you to modify the vector elements, if you don't want to allow this use the
cbegin()
and
cend()
iterators.
3. Use a range-based for loop:
1 2 3 4
|
for (auto itr : nameCountries)
{
std::cout << itr << '\n';
}
|
(This copies the vector. Recommended you use a reference to avoid needless copying:
for (auto& itr : nameCountries)
. This will let the vector be modified. To prevent modification:
for (const auto& itr : nameCountries)
)
Vectors are easy to pass into functions. Instead of having the code to PRINT your vectors in main:
1 2 3 4 5 6 7 8 9 10
|
void display(const std::vector<std::string>& countryNames, const std::vector<int>& countryTimes)
{
for (size_t loop = 0; loop < countryNames.size(); loop++)
{
std::cout << countryNames[loop] << "\t- "
<< countryTimes[loop] / 60 << " hours and "
<< countryTimes[loop] % 60 << " mins of TV daily.\n";
}
}
|
Call the function within main:
display(ctryNames, ctryMins);
China - 2 hours and 38 mins of TV daily.
India - 1 hours and 59 mins of TV daily.
Russia - 3 hours and 48 mins of TV daily.
UK - 4 hours and 2 mins of TV daily.
USA - 4 hours and 43 mins of TV daily. |
Adding an element is easy with a
push_back()
(assume starting with the original 5):
1 2 3
|
ctryNames.push_back("France");
std::cout << "The number of countries is now " << ctryNames.size() << '\n';
|
The number of countries is now 6 |
A simple deletion of an element is done with the
erase()
method:
1 2
|
// erasing the 3rd element
ctryNames.erase(ctryNames.begin() + 2);
|
Deleting an element by comparing the element's data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// erasing a specified element
std::string toErase = "Russia";
for (auto itr = ctryNames.begin(); itr != ctryNames.end();)
{
if (*itr == toErase)
{
itr = ctryNames.erase(itr);
}
else
{
itr++;
}
}
for (const auto& itr : ctryNames)
{
std::cout << itr << '\n';
}
|
China
India
UK
USA
France |
Whew! That is a lot of material to digest. :)
One last thing to consider, consider this VERY extra credit: Instead of creating two separate yet parallel vectors, create just one, using
std::pair
as the data type (make sure to include
<utility>
:
1 2 3
|
std::vector<std::pair<std::string, int>> country { std::make_pair("China", 158), std::make_pair("India", 119),
std::make_pair("Russia", 228), std::make_pair("UK", 242),
std::make_pair("USA", 283) };
|
Displaying the contents of this vector:
1 2 3 4 5 6 7
|
for (const auto& itr : country)
{
std::cout << itr.first << "\t- "
<< itr.second / 60 << " hours and "
<< itr.second % 60 << " mins of TV daily.\n";
}
|
China - 2 hours and 38 mins of TV daily.
India - 1 hours and 59 mins of TV daily.
Russia - 3 hours and 48 mins of TV daily.
UK - 4 hours and 2 mins of TV daily.
USA - 4 hours and 43 mins of TV daily. |
http://www.cplusplus.com/reference/utility/pair/