By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Pelle Beckman" <he******@chello.se> wrote in message
news:z8kxe.40$qE5.39@amstwist00...
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<MyClass*>
* 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)
}
}
Jul 23 '05 #2

P: n/a
Jim Langston skrev:
"Pelle Beckman" <he******@chello.se> wrote in message
news:z8kxe.40$qE5.39@amstwist00...
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<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.
* 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)
}
}


Thanks for you answers!
Jul 23 '05 #3

P: n/a
Pelle Beckman <he******@chello.se> wrote in news:z8kxe.40$qE5.39
@amstwist00:
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;

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.