how to get element from vector c++ code example
Example 1: get values from a vector of vectors c++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector<int> > buff;
for(int i = 0; i < 10; i++)
{
vector<int> temp;
for(int j = 0; j < 10; j++)
temp.push_back(i);
buff.push_back(temp);
}
for(int i = 0; i < buff.size(); ++i)
{
for(int j = 0; j < buff[i].size(); ++j)
cout << buff[i][j];
cout << endl;
}
return 0;
}
Example 2: vector by index c++
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector (10);
std::vector<int>::size_type sz = myvector.size();
for (unsigned i=0; i<sz; i++) myvector[i]=i;
for (unsigned i=0; i<sz/2; i++)
{
int temp;
temp = myvector[sz-1-i];
myvector[sz-1-i]=myvector[i];
myvector[i]=temp;
}
std::cout << "myvector contains:";
for (unsigned i=0; i<sz; i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
}