how do I access a member of vector when this vector is a member of a class

D

ding feng

I am a beginner. So this question could be very stupid.

Would anyone help me to solve this problem? A formatted txt file is
read. Then i need to look into a vector who is a member of a class to
search the matched string and return this string. For example, this
vector has (string1, string2, ...stringn). When the formatted txt file
is read, I get string j which could be a member of the above vector.
This vector is a private member of class obj. Please help me to build
a function in the main and get stringj from that vector.

thanks
 
H

Howard

ding feng said:
I am a beginner. So this question could be very stupid.

Would anyone help me to solve this problem? A formatted txt file is
read. Then i need to look into a vector who is a member of a class to
search the matched string and return this string. For example, this
vector has (string1, string2, ...stringn). When the formatted txt file
is read, I get string j which could be a member of the above vector.
This vector is a private member of class obj. Please help me to build
a function in the main and get stringj from that vector.

thanks

If you need to access private members of a class, then you should write a
function in that class that will search its own vector for the given string.
(I don't see why you would want to return the string, though, since it's the
same as what you asked the function to look for. Probably better to return
an index or iterator.)

(By the way, I assume when you wrote "build a function in the main" that you
meant "...in the main unit, outside the class", and not "...in the function
main()". You can't put a function inside another function in C++.)

So, you just need to add a Find( string str ) function in your class. That
function can do the search, and can use the vector's own find function to do
it. Look up find for the vector template class. (I think that's what it's
called...?)

-Howard
 
C

Chris Theis

Dhruv said:
On Fri, 27 Jun 2003 06:04:41 -0700, ding feng wrote:
[SNIP>
There you go!!!!!!!!!!!!

Sorry pal, but the way you go is not the way the OP wants to go! He wanted
to know how to access the vector which is a private member of an object from
the outside!
HTH,
-Dhruv.


My C++ wish list: A vector that has as its 2nd parameter a stream object
instead of an allocator, so that you can pass it any stream, and it uses
that as storage. Then, you can very conveniently provide a memory stream,
which will work like an allocator!!!

An what if the user passes cout or cin which are perfectly fine streams?

Regards
Chris
 
C

Chris Theis

ding feng said:
I am a beginner. So this question could be very stupid.

Would anyone help me to solve this problem? A formatted txt file is
read. Then i need to look into a vector who is a member of a class to
search the matched string and return this string.

If you got one string and try to find a matching string why would you want
to return that string which you already have?
For example, this
vector has (string1, string2, ...stringn). When the formatted txt file
is read, I get string j which could be a member of the above vector.
This vector is a private member of class obj. Please help me to build
a function in the main and get stringj from that vector.

thanks

What you need is a member function that can access the private members and
perform the search:

class CMyClass {
public:
bool FindString( const string& Str );
...
private:
vector<string> m_Data;
};

bool CMyClass::FindString( const string& Str )
{
vector<string>::iterator Iter = find( m_Data.begin, m_Data.end() );
if ( Iter == m_Data.end() )
return false;

return true;
}

HTH
Chris
 
G

Graham Cox

ding said:
I am a beginner. So this question could be very stupid.

Would anyone help me to solve this problem? A formatted txt file is
read. Then i need to look into a vector who is a member of a class to
search the matched string and return this string. For example, this
vector has (string1, string2, ...stringn). When the formatted txt file
is read, I get string j which could be a member of the above vector.
This vector is a private member of class obj. Please help me to build
a function in the main and get stringj from that vector.

thanks

you can create a public method for your obj class which takes
a string as an argument and returns what ever you want ie boolean i.e.

class obj
public:
bool search_vector_method(string& str)
{
//Include what Dhruv replied

if(vi != v.end())
{
return true;
}
else
{
return false;
}
}

//then in main create string to search for

string str2match;
cin >> str2match;

//create an object from the obj class
//and call the public method

obj obj1;
if(obj1.search_vector_method(str2match) == "no_match")
{
.....
}
else
{
.....
}

Something alonmg those lines - hope it helps!
 
D

Dhruv

[SNIP>
There you go!!!!!!!!!!!!

Sorry pal, but the way you go is not the way the OP wants to go! He
wanted to know how to access the vector which is a private member of an
object from the outside!

Ok, then just put the code in a member function.

An what if the user passes cout or cin which are perfectly fine streams?

cout and cin are objects of a stream type, not stream classes.


I get what you are asking for.

Then write and read to and from the streams, which would be the
behaviour...... Also, the stream needs to support <<, and >>.

So, we get something like this:

vector<type, stream_name>, so:

vector<int, console_stream> v1; //vector will do ONLY output to the stream in terms
of only ints. That's because only << is defined for cout. If WE write to
the stream, we have to only cout<<int. Otherwise, the stream for the
vector will get corrupted.

Now, we wish to write 2 integers to the screen:

v1.push_back (23);
v1.push_back (56);

If we wish to write these ints to a file, we just do:

vector<int, file_stream> v1;
//and same code goes here.

This will be more useful in file streams, where <<, and >> are both
defined.

Regards,
-Dhruv.
 
C

Chris Theis

Dhruv said:
[SNIP]
An what if the user passes cout or cin which are perfectly fine streams?
cout and cin are objects of a stream type, not stream classes.

Sorry, of course. I just missed that you were refering to the second
template parameter and not to the ctor.
I understand your motivation for using streams as a kind of allocators
because you'd get a nice persistency framework but there are subtle issues
which make life quite hard.
I get what you are asking for.
[SNIP]
we wish to write 2 integers to the screen:

v1.push_back (23);
v1.push_back (56);

And what will the following code do which is fine with the common vector
behavior?

for( vector<int, console_stream>::iterator Iter _ v1.begin(); Iter !=
v1.end(); ++Iter ) {
// do whatever you want with Iter
}

To access values pushed into vector<int, console_stream> they must be stored
somewhere so we'd have to have a "normal" storing vector and additionally
the ability to output to the screen.
If we wish to write these ints to a file, we just do:

vector<int, file_stream> v1;
//and same code goes here.

This will be more useful in file streams, where <<, and >> are both
defined.

That would be really nice but in principle you can do this with the help of
ifstream/ofstream iterators. The tricky thing with your solution is what
should be done if one wants to store pointers in the vector. If you write
them to a file & restore them afterwards they're not valid anymore. Thus all
the issues regarding persistency will come down on the one who has to
implement this behavior.

Don't get me wrong your idea is nice but the problem is that it would
introduce a whole new behavior which is not necessarily compatible to the
normal vector behavior. Most people consider it a good design rule that one
shouldn't pack many special cases into one "almighty" tool but to
differentiate. IMHO the stream iterators in combination with the collection
templates should suffice to solve what you want, respectively you can still
wrap them in your own framework so that the user doesn't need to know about
the details.
Regards,
-Dhruv.

Best regards
Chris
 
D

Dhruv

Dhruv said:
[SNIP]
An what if the user passes cout or cin which are perfectly fine streams?
cout and cin are objects of a stream type, not stream classes.

Sorry, of course. I just missed that you were refering to the second
template parameter and not to the ctor.
I understand your motivation for using streams as a kind of allocators
because you'd get a nice persistency framework but there are subtle issues
which make life quite hard.
Yes, it would quite make life hard for the developer, but the user would
enjoy!!!!!! I have thought about it, but it seems that it needs much more
thought than I initially anticipated...... However, I've got to learn
about streams first.
I get what you are asking for.
[SNIP]
we wish to write 2 integers to the screen:

v1.push_back (23);
v1.push_back (56);

And what will the following code do which is fine with the common vector
behavior?

for( vector<int, console_stream>::iterator Iter _ v1.begin(); Iter !=
v1.end(); ++Iter ) {
// do whatever you want with Iter
}
Yes, this has occured to me, I'll geive it some more thought as I said.
To access values pushed into vector<int, console_stream> they must be stored
somewhere so we'd have to have a "normal" storing vector and additionally
the ability to output to the screen.


That would be really nice but in principle you can do this with the help of
ifstream/ofstream iterators. The tricky thing with your solution is what
should be done if one wants to store pointers in the vector. If you write
them to a file & restore them afterwards they're not valid anymore. Thus all
the issues regarding persistency will come down on the one who has to
implement this behavior.
But, no one would use persistant storage for pointers.
Don't get me wrong your idea is nice but the problem is that it would
introduce a whole new behavior which is not necessarily compatible to the
normal vector behavior. Most people consider it a good design rule that one
shouldn't pack many special cases into one "almighty" tool but to
differentiate. IMHO the stream iterators in combination with the collection
templates should suffice to solve what you want, respectively you can still
wrap them in your own framework so that the user doesn't need to know about
the details.

Some more thought is on the cards for me. (including some more sleep).

Regards,
-Dhruv.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top