I'm looking for an efficient way to solve the following (presumably easy) problem:
I have a vector of type A.
Class A contains a vector of type B.
There is no way that I can change class A or the design of
the vector of class A (I cannot make it a vector of pointers to A
for example).
I want to have a vector of class B which contains all entries of B contained in the vector of A with a special attribute.
I guess it is a bit complicated to read about the problem but probably easier to understand if you see an example:
#include <vector>
#include <iostream>
using namespace std;
struct B {
int n;
double val;
};
struct A {
vector<B> v;
};
int main() {
// generate some dummy data:
A a1;
a1.v.push_back(B{1, 1.0});
a1.v.push_back(B{2, 2.0});
a1.v.push_back(B{1, 3.5});
A a2;
a1.v.push_back(B{2, 2.0});
a1.v.push_back(B{3, 1.0});
a1.v.push_back(B{1, 2.5});
// this is my initial situation: a vector of type A
vector<A> va;
va.push_back(a1);
va.push_back(a2);
// what I want to get is a vector of type B with all B values whose n == 1
vector<B> vb;
// possible solution to get all elements of B
for(auto &any : va){
for(auto &b : any.v){
if(b.n == 1){
vb.push_back(b);
}
}
}
for(const auto &any:vb){
cout << any.val << endl;
}
return 0;
}
Questions:
I would like to get the vector of class B without copying every element. Later, I need to modify the elements in vector B and they should also be modified in vector A (meaning, if I change vb[0].val = 100; the entry in va[0].v[0] should also be 100;
I thought about using pointers to do this but I am unsure if it is a good idea since the elements of vector va are allocated on the stack and not on the heap.
** Edit: **
I can ensure that as soon as I need the vector of B I will not modify the vector of A in any way.
Having va on the stack is not necessarily a problem, as long as you move va to its new location. In that case your "moved to" vector will internally point to the storage allocated by your "moved from" va vector (and the original va will become empty).
What is an issue though, is if you add new Bs to an A after having already created pointers to Bs. If the A's v vector does not have enough capacity to store the newly inserted B, it will have to reallocate all its B elements to a new storage location. That in turn will change the addresses of all of those B elements.
Thus, you are fine to create pointers as long as you can guarantee that:
va is moved instead of copied.
No new Bs are added to an A after initialization.
Assuming the above can be guaranteed, you can create your vb as follows:
std::vector<B *> vb;
for (auto & a : va) {
for (auto & b : a.v) {
if (b.n == 1) { vb.push_back(&b); }
}
}
or using std::reference_wrapper:
std::vector<std::reference_wrapper<B>> vb;
for (auto & a : va) {
std::copy_if(a.v.begin(), a.v.end(),
std::back_inserter(vb),
[](auto const & b) { return b.n == 1; });
}
Related
Say I have the class
class A{
int value;
public:
A(int val) : value(val) {};
}
I store pointers of instances in a collection such as vector using for loop
std::vector<A*> myCollection;
for (int i = 0; i < 10; ++i){
myCollection.push_back(&A(i));
}
Now the for loop will construct and destruct an object at the same memory location resulting in a vector with 10 pointers pointing to the same address and dereferencing them will give A->value = 9.
Is there any way around this without dynamic allocation? And yes I have to use collection of pointers and not references.
If the objects need to be on the stack, but you also want a vector of pointers because of some API requirement, etc., Just create an array of the objects, then store the pointers. Be very mindful of the lifetime issues.
size_t const sz = 3;
A arr[sz] {1, 2, 3};
std::vector<A*> v;
v.reserve(sz);
for (auto& a : arr) v.push_back(&a);
someFunc(v);
The problem with your current program is that A(i) is a prvalue and hence its address using the & operator cannot be taken. This means that the following expression is invalid in your code:
//---------------------vvvvv----->invalid because A(i) is a prvalue
myCollection.push_back(&A(i));
You could instead use a std::vector<A> in addtion to std::vector<A*> as shown below:
std::vector<A> myVector;
myVector.reserve(10);
//-------^^^^^^^---------------->to avoid reallocations when capacity is not enough
for (int i = 0; i < 10; ++i){
myVector.emplace_back(i);
//-----------^^^^^^^^^^^^------->use emplace_back to forward the argument i
}
std::vector<A*> myPtrVector;
myPtrVector.reserve(10);
for(auto&elem: myVector)
{
myPtrVector.push_back(&elem);
}
I have the following struct:
#include <string>
#include <vector>
struct A {
std::string name;
int id;
};
And a vector containing A elements:
std::vector<A> a_vector;
I am trying to append an element to the vector and change its values using the following:
void test()
{
A a;
get_a(a);
//Up to this point I thought modifying this a object would mean modifying the back element of the vector. But it doesn't work as planned, doing this:
a.id = 2; //Doesn't modify the id of the element in the vector.
}
where get_a is defined as : (The code is simplified, in the real one I really need to pass a as argument and not get it as return)
void get_a(A& a) //This function normally assigns a in different ways
{
a_vector.emplace_back();
a = a_vector.back();
}
How can I do to have the a element be the same as the one in the vector? Do I really have to use pointers?
A a;
a = a_vector.back();
Here you're copy-assigning a_vector.back() to a. This is not a reference, so modifying a will not modify the element inside the vector.
You want this instead:
A& a = a_vector.back();
If you cannot immediately initialize your reference with a_vector.back(), consider using a pointer...
A* a;
// ...
a = &a_vector.back();
// ...
something(*a);
...or an index:
std::size_t a_idx;
// ...
a_idx = a_vector.size() - 1;
// ...
something(a_vector[a_idx]);
The pointer will work fine if you know that the vector won't get resized. If the vector resize, iterators and pointers will be invalidated.
The index will work fine even if the vector gets resized, as long as the elements are not removed/shifted around.
You need a reference to the object:
auto& a = a_vector.back();
Or, in a more compact manner:
a_vector.back().id = 2;
You're holding a copy, not the original object. That is why the object in vector does not get modified.
Answer to edited question: references can be assigned only during declaration. What you want is probably std::reference_wrapper, but anyway, please don't use it unless you have to.
Consider the following code:
#include <iostream>
#include <vector>
using namespace std;
class SomeClass {
public:
SomeClass(int num) : val_(num) {}
int val_;
int val() const { return val_; }
};
// Given a vector of vector of numbers, this class will generate a vector of vector of pointers
// that point to SomeClass.
class Generator {
public:
vector<SomeClass> objects_;
vector<vector<SomeClass*> > Generate(const vector<vector<int> >& input) {
vector<vector<SomeClass*> > out;
for (const auto& vec : input) {
out.push_back({});
for (const int num : vec) {
SomeClass s(num);
objects_.push_back(s);
out.back().push_back(&objects_.back());
}
}
return out;
}
};
int main() {
Generator generator;
auto output = generator.Generate({{2, 3}, {4, 5}, {6}});
for (const auto& vec : output) {
for (const auto* obj : vec) {
printf("%d ",obj->val());
}
printf("\n");
}
return 0;
}
The Generate method in the Generator class will simply convert the vector of vector of ints to a vector of vector of pointers to SomeClass.
SomeClass is simply a container for a simple int value with a getter method.
I would expect the following output:
2 3
4 5
6
However, I get the following output:
junk_integer junk_integer
4 5
6
It seems the pointers in the first row become dangling pointers. What is wrong with this code?
You're storing pointers into a vector, then adding elements to the vector. Since you're not reserving enough space for all the elements you're adding, when the vector resizes it invalidates all the pointers to the old data.
You'll either have to reserve enough space before storing the pointers, store the pointers after you've stored everything in the vector you need to store, or not store pointers (maybe store an index, instead).
All operations that increase the number of elements in a std::vector, including push_back(), invalidates all iterators (including pointers) that refer to elements of the vector, if the resizing results in a change of vector capacity.
Your code is doing
objects_.push_back(s);
out.back().push_back(&objects_.back());
within a loop. Every call of objects_.push_back() invalidates iterators of objects_, and therefore can result in out.back() containing invalid (dangling) pointers.
You are storing pointers to objects contained in generator.objects_. Some of them become dangling pointers when you call push_back() on that.
In general, storing pointers to objects in a std::vector is a bad idea.
I'm trying to create a class which maintains a fixed size vector of unique pointers to managed objects, like so:
std::vector<std::unique_ptr<Myclass>> myVector;
The vector gets initialized like so:
myVector.resize(MAX_OBJECTS,nullptr);
Now, what I need to to, is to be able to, on request, remove one of the stored unique pointers without affecting the size of the vector.
I also need to safely add elements to the vector too, without using push_back or emplace_back.
Thanks in advance.
Edit: I want the vector to be of constant size because I want to be able to add and remove elements in constant time.
If you want a vector of fixed size, use std::array.
To remove a unique_ptr in an index, you can use std::unique_ptr::reset():
myVector[i].reset()
To add an element to a specific index (overwriting what was there before) you can use std::unique_ptr::reset() with the new pointer as parameter:
myVector[i].reset(new Myptr(myparameter));
Reading a reference may also help:
http://en.cppreference.com/w/cpp/memory/unique_ptr
http://en.cppreference.com/w/cpp/container/array
http://en.cppreference.com/w/cpp/container/vector
Looks like you want to use a std::array<> rather than forcing std::vector<> to behave like one.
As already pointed out you should use std::array if the size is fixed.
E.g like this:
std::array<std::unique_ptr<YourType>, MAX_OBJECTS> myVector;
You can then remove or add a new pointer like this.
for(auto& v : myVector)
if(v && predicate)
v.reset();// or v.reset(ptr) to set a new one
You can use STL algorithm std::remove, like this:
// all items that should be removed will be the range between removeAt and end(myVector)
auto removeAt = std::remove_if(begin(myVector), end(myVector),
ShouldRemovePredicate);
// reset all items that should be removed to be nullptr
for(auto it = removeAt; it != end(myVector); ++it)
it->reset();
In addition, if the size is known at compile-time I would suggest using std::array<unique_ptr<MyObject>, SIZE> instead of a vector. However, if SIZE is not known at compile-time your code is ok.
You could use std::array instead of a std::vector since you know the number of the elements beforehand and you could add and remove elements like the following example:
#include <iostream>
#include <memory>
#include <array>
class foo {
std::size_t id;
public:
foo() : id(0) {}
foo(std::size_t const _id) : id(_id) {}
std::size_t getid() const { return id; }
};
auto main() ->int {
// construct an array of 3 positions with `nullptr`s
std::array<std::unique_ptr<foo>, 3> arr;
// fill positions
std::unique_ptr<foo> p1(new foo(1));
arr[0] = std::move(p1);
std::unique_ptr<foo> p2(new foo(2));
arr[1] = std::move(p2);
std::unique_ptr<foo> p3(new foo(3));
arr[2] = std::move(p3);
// print array
for(auto &i : arr) if(i != nullptr) std::cout << i->getid() << " ";
std::cout << std::endl;
// reset first position (i.e., remove element at position 0)
arr[0].reset();
// print array
for(auto &i : arr) if(i != nullptr) std::cout << i->getid() << " ";
std::cout << std::endl;
return 0;
}
LIVE DEMO
Well I am questioning myself if there is a way to pass a vector directly in a parameter, with that I mean, like this:
int xPOS = 5, yPOS = 6, zPOS = 2;
//^this is actually a struct but
//I simplified the code to this
std::vector <std::vector<int>> NodePoints;
NodePoints.push_back(
std::vector<int> {xPOS,yPOS,zPOS}
);
This code ofcourse gives an error; typename not allowed, and expected a ')'
I would have used a struct, but I have to pass the data to a Abstract Virtual Machine where I need to access the node positions as Array[index][index] like:
public GPS_WhenRouteIsCalculated(...)
{
for(new i = 0; i < amount_of_nodes; ++i)
{
printf("Point(%d)=NodeID(%d), Position(X;Y;Z):{%f;%f;%f}",i,node_id_array[i],NodePosition[i][0],NodePosition[i][1],NodePosition[i][2]);
}
return 1;
}
Ofcourse I could do it like this:
std::vector <std::vector<int>> NodePoints;//global
std::vector<int> x;//local
x.push_back(xPOS);
x.push_back(yPOS);
x.push_back(zPOS);
NodePoints.push_back(x);
or this:
std::vector <std::vector<int>> NodePoints;//global
std::vector<int> x;//global
x.push_back(xPOS);
x.push_back(yPOS);
x.push_back(zPOS);
NodePoints.push_back(x);
x.clear()
but then I'm wondering which of the two would be faster/more efficient/better?
Or is there a way to get my initial code working (first snippet)?
Use C++11, or something from boost for this (also you can use simple v.push_back({1,2,3}), vector will be constructed from initializer_list).
http://liveworkspace.org/code/m4kRJ$0
You can use boost::assign as well, if you have no C++11.
#include <vector>
#include <boost/assign/list_of.hpp>
using namespace boost::assign;
int main()
{
std::vector<std::vector<int>> v;
v.push_back(list_of(1)(2)(3));
}
http://liveworkspace.org/code/m4kRJ$5
and of course you can use old variant
int ptr[1,2,3];
v.push_back(std::vector<int>(ptr, ptr + sizeof(ptr) / sizeof(*ptr));
If you don't have access to either Boost or C++11 then you could consider quite a simple solution based around a class. By wrapping a vector to store your three points within a class with some simple access controls, you can create the flexibility you need. First create the class:
class NodePoint
{
public:
NodePoint( int a, int b, int c )
{
dim_.push_back( a );
dim_.push_back( b );
dim_.push_back( c );
}
int& operator[]( size_t i ){ return dim_[i]; }
private:
vector<int> dim_;
};
The important thing here is to encapsulate the vector as an aggregate of the object. The NodePoint can only be initialised by providing the three points. I've also provided operator[] to allow indexed access to the object. It can be used as follows:
NodePoint a(5, 6, 2);
cout << a[0] << " " << a[1] << " " << a[2] << endl;
Which prints:
5 6 2
Note that this will of course throw if an attempt is made to access an out of bounds index point but that's still better than a fixed array which would most likely seg fault. I don't see this as a perfect solution but it should get you reasonably safely to where you want to be.
If your main goal is to avoid unnecessary copies of vector<> then here how you should deal with it.
C++03
Insert an empty vector into the nested vector (e.g. Nodepoints) and then use std::swap() or std::vector::swap() upon it.
NodePoints.push_back(std::vector<int>()); // add an empty vector
std::swap(x, NodePoints.back()); // swaps contents of `x` and last element of `NodePoints`
So after the swap(), the contents of x will be transferred to NodePoints.back() without any copying.
C++11
Use std::move() to avoid extra copies
NodePoints.push_back(std::move(x)); // #include<utility>
Here is the explanation of std::move and here is an example.
Both of the above solutions have somewhat similar effect.