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
}
Related
I have got a std::vector X of std::vector of, say, double in C++.
How can I transform X into a std::vector Y of int such that X[i].size() == Y[i] holds for all admissible indices i?
std::vector< std::vector<int> > X;
...
/* What I want to do should look as follows */
std::vector<int> Y = std::copy_and_transform( X, lambda_to_get_size );
Of course, this can be realized with a loop, but in C++11 we would like to use lambdas instead. I have not found anything like that in std::algorithm. The standard tools seem to only give in place transformations that change the original vector and which do not allow changing the data type.
You can use std::transform:
std::vector<int> Y;
std::transform(X.cbegin(), X.cend(), std::back_inserter(Y), [](const std::vector<int>& value) {
return value.size();
});
How to create a vector of matrices (vector of vectors) where the matrices are of different size and initialized?
typedef std::vector<double> Vector;
typedef std::vector<std::vector<double>> Matrix;
Vector v;
std::unique_ptr<Matrix> m = std::make_unique<Matrix>();
(*m)[0][0] = 1.0;
v.push_back(m);
Compilation error:
vectors.cpp: In function 'int main()':
vectors.cpp:37:18: error: no matching function for call to 'std::vector<double>::push_back(std::uniq
ue_ptr<std::vector<std::vector<double> > >&)'
v.push_back(m);
^
You need to use something along the lines of:
typedef std::vector<std::vector<double>> Matrix;
typedef std::vector<std::unique_ptr<Matrix>> MatrixVector;
std::unique_ptr<Matrix> m = std::make_unique<Matrix>();
MatrixVector mv;
mv.push_back(std::move(m));
Change type Vector to:
typedef std::vector<std::unique_ptr<Matrix>> Vector;
then you can push_back it,
v.push_back(std::move(m));
BTW: (*m)[0][0] = 1.0; is UB. You might use push_back to add element.
Arrange your typedefs thus:
typedef std::vector<std::vector<double>> Matrix;
typedef std::vector<std::shared_ptr<Matrix>> Vector;
Note the use of shared_ptr - this makes it easier to transfer ownership of the Matrix into the Vector.
Then:
Vector v;
std::shared_ptr<Matrix> m = std::make_shared<Matrix>();
// Add content to the matrix...
v.push_back(m);
Could you guys Please help me in providing good notes or links ?
For Ex : I need to create a vector and dump these x,y values in Vector ..
Data { X , Y } = {1,1} , {1,2} , {1,3}, {2,1},{2,2},{2,3},{3,1},{3,2},{3,3}
A vector of point in OpenCV is just a standard C++ STL vector containing OpenCV Point objects :
std::vector<Point> data;
data.push_back(Point(1,1));
data.push_back(Point(1,2));
data.push_back(Point(1,3));
data.push_back(Point(2,1));
...
Alternatively, if you're using C++11 or later you can use a list initialization:
std::vector<Point> data = {Point(1,1), Point(1,2), Point(1,3), Point(2,1)};
Take a look at the C++ reference for STL Vector
So... you want to use a vector to store data... wherein each element is a pair of ints? Well, if you don't want to create your own type, use a tuple or pair:
#include <vector>
#include <utility>
// ...
std::vector<std::pair<int, int> v;
// ...
v.push_back(std::make_pair(1, 1));
// ...
auto p = c[offset];
int x = p.first;
int y = p.second;
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.
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());