i'm trying to extract a subvector of int from another subvector of int using iterator with this code :
std::vector<int> Original;
std::vector<int> NewVec;
NewVec(Original.begin(), Original.begin()+5);
and this doesn't work, only if i declare NewVec in the same line of extraction like that :
std::vector<int> NewVec(Original.begin(), Original.begin()+5);
So, is it possible to use NewVec such that its declaration is before its construction ?
Thanks.
If you want to assign a subrange to an existing std::vector you can use the assign() member:
NewVec.assign(Original.begin(), Original.begin() + 5);
Of course, this assumes that Original has at least 5 elements.
You can use std::copy:
NewVec.reserve(5);
std::copy(std::begin(Original), std::begin(Original)+5,
std::back_inserter(NewVec));
Or std::vector::assign:
NewVec.assign(std::begin(Original), std::begin(Original)+5);
Related
If I wanted to copy the same value across an iterator range, I would think that it would be easy to have a noop iterator where you pass it a value, and when it is incremented, it would not move anywhere. This would allow using the existing std::copy and std::copy_if algorithms.
However, I can't seem to find such a beast. Am I going to have to roll my own?
Use std::fill or std::fill_n algorithm.
Some containers, e.g. std::vector<> and std::list<>, have a constructor with size and initializer:
std::vector<int> v(10, 42); // 42 is the initializer
v.resize(20, 42); // Initialize new elements with 42.
As far as I know there is no iterator for this but there is an algorithm. std::generate will take a range an assign a value to each element that is returned from the generator passed to it. If you want to assign everything 42 for instance that would look like
std::vector<int> vec(20);
std::generate(vec.begin(), vec.end(), []() { return 42; });
You can even get values that change like
std::vector<int> vec(20);
std::generate(vec.begin(), vec.end(), []() { static int counter = 0; ++counter; return counter * counter; });
I need to convert a std::array to a std::vector, but I could not find anyway to do it quickly. Here is the sample code:
std::array<char,10> myData={0,1,2,3,4,5,6,7,8,9};
Now I need to create a vector such as:
std::vector<char> myvector;
and initialize it with the array values.
What is the fastest way to do this?
You can use the constructor of std::vector taking iterators.
Constructs the container with the contents of the range [first, last).
e.g.
std::array<char,10> myData = {0,1,2,3,4,5,6,7,8,9};
std::vector<char> myvector(myData.begin(), myData.end());
Just for variety:
std::vector<char> myvector(std::begin(myData), std::end(myData);
std::vector<char> myvector { myData.begin(), myData.end() };
I'd use the range constructor of vector - looking like myvector(myData.begin(), myData.end())
for future reference:
http://en.cppreference.com/w/cpp/container/vector/vector
I have the following object
std::vector<std::vector<std::string>> vectorList;
Then I add to this using
std::vector<std::string> vec_tmp;
vec_tmp.push_back(strDRG);
vec_tmp.push_back(strLab);
if (std::find(vectorList.begin(), vectorList.end(), vec_tmp) == vectorList.end())
vectorList.push_back(vec_tmp);
The std::vector<std::string>s contained vectorList are only ever 2-dimensional and there are no duplicates. This works great, but I now only want to check if vectorList contains an item that index zero equal to the current strDrg. In C# I would not even be thinking about this, but this does not seem straight forward using C++. How can I find if a vector exists in vectorList where strDrg already exists in vectorList.at(i)[0]?
Note: I can use boost.
Use find_if with a lambda:
std::find_if(vectorList.begin(), vectorList.end(),
[&strDrg](const std::vector<std::string>& v) { return v[0] == strDrg; });
It seems you don't need the full power of vector for you inner elements. Consider using:
std::vector<std::array<std::string, 2>>
instead.
For doing exactly what you asked, std::find_if with a lambda as #chris proposed in comments is the best:
std::find_if(ob.begin(), ob.end(),
[&](const auto x){return x[0] == strDRG;});
// Replace auto with "decltype(ob[0])&" until
//you have a C++1y compiler. Might need some years.
But if you only ever have exactly two elements, consider using a std::array<...>, a std::pair<...> or a std::tuple<...> instead of the inner vector.
For tuple and pair, you need to access the first element differently:
pair : member first
tuple: use get<0>(x);
I know in C++11 I can construct a vector with a syntax like:
vector <int> a = {1,2,3,4,5};
but is it possible without looping in a similar way to initialise the vector for a number of equal elements ?
e.g.
int n= 5;
vector <string> a = (n, {"bbb"});
Yes,
vector<string> a(n, "bbb");
This works in C++03 too.
You're almost there. There's a constructor to specify the size and (optionally) a value to initialise the elements with:
vector<string> a(n, "bbb");
I looked everywhere in s.o. and then the answer was just in the c++ ref:
http://www.cplusplus.com/reference/vector/vector/vector/
It should be as simply as:
int n= 5;
vector<string> a (n,"bbb");
Is there an easy way to add all the elements of a vector to an unordered_set? They are of the same type. Right now, I am using a for loop and was wondering if there is a better way to do it
If you're constructing the unordered_set then:
std::vector<int> v;
std::unordered_set<int> s(v.begin(), v.end());
Forgive me if my syntax has any minor bugs, but you can try the std::copy function, its meant for this purpose.
std::vector<int> v;
std::unordered_set<int> s;
std::copy(v.begin(),v.end(),std::inserter(s,s.end()));