 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
ding feng Guest
|
Posted: Fri Jun 27, 2003 1:04 pm Post subject: how do I access a member of vector when this vector is a mem |
|
|
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
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Fri Jun 27, 2003 3:13 pm Post subject: Re: how do I access a member of vector when this vector is a |
|
|
"ding feng" <d.f.zhou (AT) lboro (DOT) ac.uk> wrote
| Quote: | 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
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Fri Jun 27, 2003 3:14 pm Post subject: Re: how do I access a member of vector when this vector is a |
|
|
"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote
| Quote: | 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!
| Quote: | 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
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Fri Jun 27, 2003 3:20 pm Post subject: Re: how do I access a member of vector when this vector is a |
|
|
"ding feng" <d.f.zhou (AT) lboro (DOT) ac.uk> wrote
| Quote: | 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?
| Quote: | 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
|
|
| Back to top |
|
 |
Graham Cox Guest
|
Posted: Fri Jun 27, 2003 3:25 pm Post subject: Re: how do I access a member of vector when this vector is a |
|
|
ding feng wrote:
| Quote: |
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!
|
|
| Back to top |
|
 |
Graham Cox Guest
|
Posted: Fri Jun 27, 2003 3:27 pm Post subject: Re: how do I access a member of vector when this vector is a |
|
|
Graham Cox wrote:
Whoops - small correction in last if statment!
| Quote: |
ding feng wrote:
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))
{
....
}
else
{
....
}
Something alonmg those lines - hope it helps!
|
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Fri Jun 27, 2003 5:55 pm Post subject: Re: how do I access a member of vector when this vector is a |
|
|
| Quote: | [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.
| Quote: | 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?
|
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<
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
//and same code goes here.
This will be more useful in file streams, where <<, and >> are both
defined.
Regards,
-Dhruv.
|
|
| Back to top |
|
 |
Chris Theis Guest
|
Posted: Sat Jun 28, 2003 3:57 pm Post subject: Re: how do I access a member of vector when this vector is a |
|
|
"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote
| Quote: |
[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.
| Quote: |
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.
| Quote: |
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.
Best regards
Chris
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Wed Jul 02, 2003 8:06 am Post subject: Re: how do I access a member of vector when this vector is a |
|
|
On Sat, 28 Jun 2003 17:57:55 +0200, Chris Theis wrote:
| Quote: |
"Dhruv" <dhruvbird (AT) gmx (DOT) net> wrote in message
news:pan.2003.06.27.16.58.40.359134 (AT) gmx (DOT) net...
[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.
| Quote: |
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. |
| Quote: | 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.
But, no one would use persistant storage for pointers. |
| Quote: | 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.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|