Re: Query regarding iterators and vector
In article <62d91254-dff3-4359-90f8-5da22195d8c6
@d45g2000hsc.googlegroups.com>, prasadmpatil@gmail.com says...
On Jul 5, 6:31 pm, "Daniel T." <danie...@earthlink.net> wrote:
Prasad Patil <prasadmpa...@gmail.com> wrote:
On Jul 5, 9:52 am, "Daniel T." <danie...@earthlink.net> wrote:
Jerry Coffin <jcof...@taeus.com> wrote:
I still think it's better to directly express what you're doing w=
ith an
algorithm though.
Agreed.
Thank you all for the suggestions.
What I am actually trying to do is generate a state space. I start
with a initial state and then based on rule matches generate
additional states. I continue doing this until no new states are
generated. I am storing these states in a vector. Is this a good idea=
?
Based on all the above replies there are multiple ways of doing this
namely
1. using vector.reserve()
2. using list
3. using a loop variable.
Which is the best way to implement this?
Jerry pointed the way to the best solution. For any particular "state
space" there is a query, how many new states should be generated, and a
command, generate X states.
The loop you presented tried to both determine the number of new states
and generate them at the same time, and I think that is doing too much.
Better would be to have two separate functions, one that determines the
new states to generate, and another that actually does it.
Thanks Daniel and Jerry for the advice. I did implement the algorithm
the way you suggested.
I have another newbie question.
-----------------------------
class b{
public:=09b(int val)
=09{
=09=09var = val;
=09}
int var;
};
class a{
=09public:
=09=09a(vector<b> vals)
=09{
=09=09objB= vals;
=09}
=09=09vector<b> objB;
};
int main()
{
=09vector<a> vec_of_a;
=09vector<b> temp1;
=09temp1.push_back(b(1));
=09temp1.push_back(b(2));
=09temp1.push_back(b(3));
=09vector<b> temp2;
=09temp2.push_back(b(1));
=09temp2.push_back(b(2));
=09temp2.push_back(b(3));
=09vec_of_a.push_back(a(temp1));
=09vec_of_a.push_back(a(temp2));
}
//If I need to add another element to temp2 after it has been pushed
to vec_of_a, how do I do it? because as I understand iterators don't
allow me to modify the elements of the vector.
Yes and no. Iterators allow you to modify the individual elements --
i.e. an iterator gives you access to an element, and you can modify that
element by writing to it.
A normal iterator does NOT give you access to the container that holds
the element. If you want to add an item to the container, you need some
access to the container, such as using the container's push_back member,
or creating some sort of insert iterator for the container (e.g.
insert_iterator, back_insert_iterator, etc.)
As you've defined things right now, the objB member of class a is
public, so you can do something like this:
vec_of_a.push_back(a(temp2));
// add element to vec_of_a[0]
vec_of_a[0].objB.push_back(b(4));
That's probably not a good idea though -- generally speaking, a class
shouldn't give direct access to its internal data. If you want class a
to act like a vector, you could provide a vector-like interface to do
so.
class a {
=09vector<b> objB;
public:
=09a(vector<B> const &vals) : objB(vals) {}
=09void push_back(b newB) { objB.push_back(newB); }
};
Note that passing a vector by value is (at least potentially) quite
expensive, so for this situation I've passed it by reference (to const)
instead.
Also note that since the ctors for neither a nor b is explicit, all the
conversions can be done implicitly:
int main() {
=09vector<a> vec_of_a;
=09vector<b> temp1;
=09temp1.push_back(1);
=09temp1.push_back(2);
=09temp1.push_back(3);
=09vector<b> temp2;
=09temp2.push_back(1);
=09temp2.push_back(2);
=09temp2.push_back(3);
=09vec_of_a.push_back(temp1);
=09vec_of_a.push_back(temp2);
=09
=09return 0;
}
Of course it may be open to question whether 1) the explicit cases are
clearer, and/or 2) you might want to make the ctors explicit, so these
conversions won't happen by accident.
--
Later,
Jerry.
The universe is a figment of its own imagination.