I would like to access elements of a vector in C++. I have generated the vector using the Boost_variant library since I needed to store both int
and string
types as inputs.
Now I would like to access elements of the vector by index, and in reverse so that I can implement a condition on them - something of the sort:
for (int i = last_element_of_vector, i >=0, i--){
if (myvec[i] == 0 && myvec[i-1] == 1){
*do something*
}
}
I can only seem to find iterators with loop over the vector and print out the elements without any index i for which the elements may be accessed.
My MWE is as follows:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
#include <boost/assign/std/vector.hpp>
#include <boost/variant.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <algorithm>
using namespace std;
using namespace boost::adaptors;
using namespace boost::assign;
typedef boost::variant<std::string,int> StringOrInt;
int main()
{
vector<StringOrInt> bools;
bools += 0, 0, 0, 0, 1, 0, 1, 1, 1, 1;
boost::copy(
bools | reversed,
std::ostream_iterator<StringOrInt>(std::cout, ", "));
return 0;
}
where the last few lines in the main only prints out the elements in the vector bools
without actually providing an index to access the elements.
Thanks in advance!