Reputation: 1389
I'm interested in:
I would like to solve this with:
Now after reading the documentation I was trying this for the iterator
part with the while
#include <iostream>
#include <vector>
class A{
public:
A(){}
~A(){}
private:
int n;
double d;
float f;
std::string s;
};
int main(){
std::vector<A*> v(100); // fixed *A to A*
std::vector<A*>::iterator iter = v.begin(); // fixed *A to A*
while( iter != v.end() )
{
*iter = new A(); // iter = A(); by the way this does not works either // now fixed using new
++iter;
}
return(0);
}
i know that this is trivial but to me it's not, in my understanding iter it's a pointer and needs to be indirected to the real value that it is pointing in the vector; clearly this concept doesn't work this way.
With the lambda and the for_each i just don't get how to use a constructor for a custom defined class because the documentation only talks about generic methods and functions and seems like i can't use a constructor.
How i can build an object with iterators and lambdas ?
Also, there is a faster way to cycle the entire vector when i need to perform the same action all over the place without using the iteration approach with the while
and the for_each
or the for
.
I would like to avoid the copy constructor overhead so i would like to keep all possible solutions using pointers.
Thanks.
Upvotes: 0
Views: 1007
Reputation: 6914
First you should use A*
instead of *A
to create pointers in C++ and second you can't set a pointer to an instance that created in stack using A()
instead you should create the value on the heap or use address of a variable from stack, so you should have *iter = new A()
Upvotes: 3