Refs. in std::vector, ctor arguments, etc

P

Pelle Beckman

Hi all,

I have a few - beginners - questions:

* Why can't I put object references in a std::vector,
i.e std::vector<MyClass&> ?
At least in doesnt work in gcc (mingw, win32)

* What's the difference between passing
member inits in the c-tor funtion from
doing them as "ordinary" vars?

i.e
MyClass::MyClass : my_member1 (5), my_member (2) { }
as opposed to
MyClass::MyClass { my_member1 = 5; my_member = 2; }

* I'm looping through a std::vector using iterators.
Under some criterias I want to access certain elements
of that vector using the following code:

std::vector<MyClass>::iterator it = MyVector.begin();
int i = 0;
while (it != MyVector.end()) {
if (some_condition) {
MyVector.at(i) = some_value;
}
i++;
it++;
}

Isn't there some way to do this without
using the somewhat ugly "i" value? Is there some
way of using the iterator as a vector element "indexer"?

Thanks,
-- Pelle
 
J

Jim Langston

Pelle Beckman said:
Hi all,

I have a few - beginners - questions:

* Why can't I put object references in a std::vector,
i.e std::vector<MyClass&> ?
At least in doesnt work in gcc (mingw, win32)

I don't know. Just store the pointer. std::vector said:
* What's the difference between passing
member inits in the c-tor funtion from
doing them as "ordinary" vars?

i.e
MyClass::MyClass : my_member1 (5), my_member (2) { }
as opposed to
MyClass::MyClass { my_member1 = 5; my_member = 2; }

Some things can only be initialized in the ctor initialization list.
Constant values being one of them. Other classes being another.

I.E.

class MyClass
{
const int MyInt;
SomeClass MyObject; <-- No default ctor
MyClass():MyInt(10), MyObject(20) {}; <-- Works
MyClass() { MyInt = 10; }; <-- Doesnt' work (Can't even figure
.. how you'd try to
initialize MyObject
}

I've also been told that it is 3x's faster to initialize them in the
initializer list. Not sure if this is true or not.
* I'm looping through a std::vector using iterators.
Under some criterias I want to access certain elements
of that vector using the following code:

std::vector<MyClass>::iterator it = MyVector.begin();
int i = 0;
while (it != MyVector.end()) {
if (some_condition) {
MyVector.at(i) = some_value;
}
i++;
it++;
}

Isn't there some way to do this without
using the somewhat ugly "i" value? Is there some
way of using the iterator as a vector element "indexer"?

Yes. *it would refer to the object instance.

std::vector<MyClass>::iterator it;
for ( it = MyVector.begin(); it != MyVector.end(); ++it )
{
if ( some_condition )
{
*it = some_value; //or
*it.ObjectMethod(10)
}
}
 
P

Pelle Beckman

Jim Langston skrev:
I don't know. Just store the pointer. std::vector<MyClass*>

Well, I'd like to but I want to be able
to delete the objects in the vector later
and references seem nicer.

Besides, I suck at C++ pointer syntax.
Some things can only be initialized in the ctor initialization list.
Constant values being one of them. Other classes being another.

I.E.

class MyClass
{
const int MyInt;
SomeClass MyObject; <-- No default ctor
MyClass():MyInt(10), MyObject(20) {}; <-- Works
MyClass() { MyInt = 10; }; <-- Doesnt' work (Can't even figure
. how you'd try to
initialize MyObject
}

I've also been told that it is 3x's faster to initialize them in the
initializer list. Not sure if this is true or not.




Yes. *it would refer to the object instance.

std::vector<MyClass>::iterator it;
for ( it = MyVector.begin(); it != MyVector.end(); ++it )
{
if ( some_condition )
{
*it = some_value; //or
*it.ObjectMethod(10)
}
}

Thanks for you answers!
 
A

Andre Kostur

Hi all,

I have a few - beginners - questions:

* Why can't I put object references in a std::vector,
i.e std::vector<MyClass&> ?
At least in doesnt work in gcc (mingw, win32)

Because references are not objects.
* What's the difference between passing
member inits in the c-tor funtion from
doing them as "ordinary" vars?

i.e
MyClass::MyClass : my_member1 (5), my_member (2) { }
as opposed to
MyClass::MyClass { my_member1 = 5; my_member = 2; }

On simple types, probably not much. On more complex types, the second
form would require your program to default construct the object first,
and then call operator= on it. The first would only be a constructor
call.
* I'm looping through a std::vector using iterators.
Under some criterias I want to access certain elements
of that vector using the following code:

std::vector<MyClass>::iterator it = MyVector.begin();
int i = 0;
while (it != MyVector.end()) {
if (some_condition) {
MyVector.at(i) = some_value;
}
i++;
it++;
}

Isn't there some way to do this without
using the somewhat ugly "i" value? Is there some
way of using the iterator as a vector element "indexer"?

Sure...

*it = some_value;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top