Reputation: 4550
In my program, I have a vector of vector of ints. Now I want to take one vector from the vector of vectors and have it manipulated in another vector container, but I get the error...
|error: conversion from '__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >' to non-scalar type 'std::vector<int, std::allocator<int> >' requested|
An example of what I am trying to do is as follows....
#include <vector>
using namespace std;
vector<vector<int> > k (13,5);
void some_funct() {
vector<int> new_v (k[2].begin(), k[2].end()); //This line is what throws the error
//here I do some stuff with new_v (e.g. sort it)
}
I'm not sure what I am doing wrong. I tried a couple of things like assigning the begin() and end() iterators to const iterator types... vector<int>::const_iterator it = k[2].begin();
but that didn't work either.
This should work (because k[x] would be a vector) but I don't know what is going wrong. Any help is appreciated!
EDIT:
After revision of my code, I noticed that there actually was an error. Instead of doing vector<int> new_v (k[2].begin(),k[2].end());
I did vector<int> new_v = (k[2].begin(),k[2].end());
.
I would like to thank Rob for giving me the initiative to copy and paste my code into SO, where I noticed my mistake.
Thank you for your help!
Upvotes: 0
Views: 142
Reputation: 1989
The error message tells us that you're trying to (re-)create a vector from a vector::iterator. since vector does not support this kind of constructor or copy assignment, the compiler would raise an error. However your posted code here is valid.
Upvotes: 0
Reputation: 168836
It is hard to know, because you haven't posted your actual code into the question. I suspect that you mis-copied the code from your project into Stack Overflow.
The offending line in your project looks something like this:
vector<int> new_v = (k[2].begin(), k[2].end());
Note the extra =
.
You are initializing new_v
with an expression of type vector::iterator
, which won't work. The statement you typed into SO, however, will work:
vector<int> new_v (k[2].begin(), k[2].end());
As will this:
vector<int> new_v = vector(k[2].begin(), k[2].end());
Or either of these:
vector<int> new_v(k[2]);
vector<int> new_v = k[2];
See https://ideone.com/uK8Xg and the corresponding error message.
Upvotes: 2