When I assign int to vector I get an error says "conversion from 'int' to non-scalar type 'std::vector<int, std::allocator<int> >' requested", what should I do?
I have vector varr(4, -1); what is the right way to do "varr[2] = 3"?
They're two different types. If you want to add an int to a vector<int> do something like:
std::vector<int> vec;
vec.push_back(10);
Update: To set an element within the vector:
std::vector<int> vec(16, 0); // Create a 16 element vector containing all 0's
vec[4] = 10; // Sets the 5th element (0 based arrays) to 10
There appears to be a thorough codeguru tutorial which might be of interest.
A vector is a collection of ints. You can not assign an int to the collection, you add it to the collection using the push_back() function:
std::vector<int> manyInts;
int oneInt = 42;
manyInts.push_back(oneInt);
If you want to add the int to to a vector<int> you should use push_back:
vector<int> v;
int i = 5;
v.push_back(i);
int sum = 468;
vector<int> v;
while(sum!=0){
v.push_back(sum%10);
sum /= 10;
}
reverse(v.begin(), v.end());
Related
I have a 2D vector vector<vector<int> > det; and I want to assign the last value or the most recently push_back'd value to another 2D vector v. This is what I tried v = *(det.rbegin()); but it shows an error. What is the correct way of doing it ?
error message : conversion from 'std::vector' to non-scalar type 'std::vector >' requested
The last entry of your 2D array is not a 2D array, i.e. of type vector < vector < int > >, but of type vector < int > .
As such it cannot be assigned to another variable of type
vector < vector < int > >
This works fine:
#include<iostream>
#include<vector>
int main(){
std::vector<std::vector<int> > det, det2;
std::vector<int> vector1, vector2, vector3;
vector1.push_back(3);
vector1.push_back(4);
vector1.push_back(5);
vector2.push_back(13);
vector2.push_back(14);
vector2.push_back(15);
vector3.push_back(113);
vector3.push_back(114);
vector3.push_back(115);
std::vector<int> member_of_2darray;
det.push_back(vector1);
det.push_back(vector2);
det.push_back(vector3);
det2.push_back(det.back());
std::cout<<det2[0][2]<<std::endl; // returns 115
//last:element
std::cout<<det.back().back()<<std::endl;
}
edit: if you deperately want to use iterators: here is an working example:
#include<iostream>
#include<vector>
int main(){
std::vector<std::vector<int> > det, det2;
std::vector<int> vector1, vector2, vector3;
std::vector<std::vector<int> >::iterator it;
vector1.push_back(3);
vector1.push_back(4);
vector1.push_back(5);
vector2.push_back(13);
vector2.push_back(14);
vector2.push_back(15);
vector3.push_back(113);
vector3.push_back(114);
vector3.push_back(115);
std::vector<int> member_of_2darray;
det.push_back(vector1);
det.push_back(vector2);
det.push_back(vector3);
it = det.end();
det2.push_back(*(it-1));
std::cout<<det2[0][2]<<std::endl; //output: 115
}
I am trying to initialize a pointer (*vectorName) with a 2D vector 366 by 4.
Both
vector<int> *vectorName = new vector<int>(366, new vector<int>(4));
and
vector<int> *vectorName = new vector<int>(366, vector<int>(4));
do not work and give me the error
Error: no instance of constructor "std::vector, <_Ty, _Alloc>::vector [with_ty=int, _Alloc=std_allocator]"
argument types are (const int, std::vector>*)
What can I do?
This is happening within the main function.
vector<int> *vectorName = new vector<int>(366, vector<int>(4));
The above doesn't work because the vector constructor template (ignoring a few things) looks as follows:
vector <TYPE> (SIZE, variable of type TYPE);
And in vector<int>(366, vector<int>(4)), vector <int> (4) is not of type int.
To create a vector with 366 elements that are vector of ints of size 4:
vector<vector<int> > *vectorName = new vector<vector<int> >(366, vector<int>(4));
or, if you don't need a pointer: (which you quite possibly don't)
vector<vector<int> > vectorName(366, vector<int>(4));
As a side note, if it's a fixed size 2D vector, why are you using vector, and not just an array. This would be much simpler:
int arr[366][4];
I assume that you're trying to use a pointer to a vector in order to get a dynamic C-array of vectors. There's no reason to do that. You can use a vector of vectors instead:
vector< vector<int> >
You can initialize that like this:
vector< vector<int> > vec(4);
for (size_t i = 0; i < 4; ++i) {
vec[i].resize(366);
}
Usage example:
vec[3][365] = 3;
If however you really do want a pointer to such a vector:
vector< vector<int> >* vec = new vector< vector<int> >(4);
for (size_t i = 0; i < 4; ++i) {
(*vec)[i].resize(366);
}
(*vec)[3][365] = 3;
If your size is constant and you're using a C++11 compiler, you can use an easier way, std::array:
array<array<int, 366>, 4> arr;
// Usage example
arr[3][365] = 3;
Or a pointer to a dynamically allocated one:
array<array<int, 366>, 4>* arr = new array<array<int, 366>, 4>;
// Usage example
(*arr)[3][365] = 3;
Are you trying to do this? This makes a 2D vector of vectors with int. It creates 366 vectors with a vector with size 4,where all items are initialized to zero. Now you have a 2D vector of 366 by 4.
vector<vector<int> > twod( 366, vector<int>(4,0));
and if you would like a pointer to this vector:
vector<vector<int> >* twodptr = &twod;
if you would really need this.
If I declare
vector< vector <double > > V;
how can I insert (3.0, 4.0) as the first element?
You can use std::make_pair to insert a pair of doubles into your vector:
std::vector<std::pair<double, double>> V;
V.push_back(std::make_pair(3.0, 4.0));
You cant add 2 values to that 2d vector, that 2d vector only store one value of type double in M x N positions.
what you can do is create a struct for example:
struct SPoint
{
double X;
double Y;
};
and then add the points like this:
vector< vector <SPoint > > v;
SPoint point = {3.0, 3.4};
v.push_back ( std::vector<SPoint >()
v[0].push_back(point);
hope it helps.
In C++03:
vector<vector<double>> V;
V.push_back(std::vector<double>(2));
V[0][0] = 3.0;
V[0][1] = 4.0;
In C++11:
vector<vector<double>> V;
V.emplace_back({3.0, 4.0});
If instead, it makes more sense to use a std::vector of std::pairs, you can do it like so:
std::vector<std::pair<double, double>> V;
V.push_back({3.0, 4.0});
Accessing an element of the pair will look like either V[0].first or V[1].second.
It isn't clear whether you want to insert an element into a pre-existing vector, or into a new, empty one. In either case, you can use the std::vector std::initializer_list constructor to initialize or assign as appropriate:
#include <vector>
int main()
{
std::vector<std::vector<double>> v1;
v1.push_back({3.0,4.0}); // push first element using initializer_list
std::vector<std::vector<double>> v2(10);
v2[0] = {3.0, 4.0}; // assign value to pre-existing vector
}
Is there any problem with my code ?
std::vector<int[2]> weights;
int weight[2] = {1,2};
weights.push_back(weight);
It can't be compiled, please help to explain why:
no matching function for call to ‘std::vector<int [2], std::allocator<int [2]> >::push_back(int*&)’
The reason arrays cannot be used in STL containers is because it requires the type to be copy constructible and assignable (also move constructible in c++11). For example, you cannot do the following with arrays:
int a[10];
int b[10];
a = b; // Will not work!
Because arrays do not satisfy the requirements, they cannot be used. However, if you really need to use an array (which probably is not the case), you can add it as a member of a class like so:
struct A { int weight[2];};
std::vector<A> v;
However, it probably would be better if you used an std::vector or std::array.
You cant do that simply.
It's better you use either of these:
vector <vector<int>> (it's basically a two dimensional vector.It should work in your case)
vector< string > (string is an array of characters ,so you require a type cast later.It can be easily.).
you can declare an structure (say S) having array of int type within it i.e.
struct S{int a[num]} ,then declare vector of
vector< S>
So indirectly, you are pushing array into a vector.
Array can be added to container like this too.
int arr[] = {16,2,77,29};
std::vector<int> myvec (arr, arr + sizeof(arr) / sizeof(int) );
Hope this helps someone.
Arrays aren't copy constructable so you can't store them in containers (vector in this case). You can store a nested vector or in C++11 a std::array.
You should use std::array instead of simple array:
#include <vector>
#include <array>
std::vector<std::array<int, 2>> weights;
std::array<int, 2> weight = {1, 2};
weights.push_back(weight);
or with a constructor:
std::vector<std::array<int, 2>> weights;
weights.push_back(std::array<int, 2> ({1, 2});
One possible solution is:
std::vector<int*> weights;
int* weight = new int[2];
weight[0] =1; weight[1] =2;
weights.push_back(weight);
Just use
vector<int*> .That will definitely work.
A relevant discussion on the same topic : Pushing an array into a vector
Situation like:
int arr[3] = { 1, 2, 3 };
std::vector<int[]> v;
v.push_back(arr);
doesn't work with error "cannot initialize array in vector with .."
This, could be worked well
int * arr = new int[3] { 1, 2, 3 };
std::vector<int*> v;
v.push_back(arr);
To instantiate the vector, you need to supply a type, but int[2] is not a type, it's a declaration.
I want to use a special method to initialize a std::vector<unsigned int> which is described in a C++ book I use as a reference (the German book 'Der C++ Programmer' by Ulrich Breymann, in case that matters). In that book is a section on sequence types of the STL, referring in particular to list, vector and deque. In this section he writes that there are two special constructors of such sequence types, namely, if Xrefers to such a type,
X(n, t) // creates a sequence with n copies of t
X(i, j) // creates a sequence from the elements of the interval [i, j)
I want to use the second one for an interval of unsigned int, that is
std::vector<unsigned int> l(1U, 10U);
to get a list initialized with {1,2,...,9}. What I get, however, is a vector with one unsigned int with value 10 :-| Does the second variant exist, and if yes, how do I force that it is called?
there are at least three ways that you can do that. One was mentioned earlier by Brian
//method 1
generate(v.begin(), v.end(), [] { static int i {1}; return i++; });
You can also use std::iota if you are using c++11
//method 2
iota(v.begin(), v.end(), 1);
Or instead you can initialize your container with 1s and then do a partial sum on that. I don't think anybody will use this third method anyway :)
//method 3
vector<int> v(n, 1);
partial_sum(v.begin(), v.end(), v.begin());
Reread the paragraphs near there describing what each of the parameters are. Specifically, it should mention that i and j are not values, but iterators. This constructor is very commonly used to make copies of other types of containers. If you want to get a sequence of values, the Boost library provides a counting iterator, that does exactly what you want.
std::vector<unsigned int> numbers(
boost::counting_iterator<unsigned int>(0U),
boost::counting_iterator<unsigned int>(10U));
A non-boost way to do this with a self-incrementing iterator.
#include <vector>
#include <iostream>
#include <algorithm>
static int NUM_ITEMS = 10;
class gen_range {
public:
gen_range(int i) { idx = i; }
int operator()() { return (idx++); };
int idx;
};
int main() {
std::vector<int> x(NUM_ITEMS);
std::generate_n(x.begin(), NUM_ITEMS, gen_range(0));
for (int i=0; i < x.size(); i++) {
std::cout << x[i] << std::endl;
}
}
C++11:
std::vector<int> idxs (n);
std::generate_n (idxs.begin (), n, [] { static int i {1}; return i++; });
No, that variant does not exist. The second constructor initializes a vector from two iterators that point into another sequence.
Here is an example of the "two-iterator" constructor in action:
int fill_data[4] = { 1, 2, 3, 4 };
std::vector<int> v(fill_data, fill_data + 4);