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

Discussion in 'C++' started by ding feng, Jun 27, 2003.

  1. ding feng

    ding feng Guest

    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
    ding feng, Jun 27, 2003
    #1
    1. Advertising

  2. ding feng

    Howard Guest

    "ding feng" <> wrote in message
    news:...
    > 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
    Howard, Jun 27, 2003
    #2
    1. Advertising

  3. ding feng

    Chris Theis Guest

    "Dhruv" <> wrote in message
    news:p...
    > 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
    Chris Theis, Jun 27, 2003
    #3
  4. ding feng

    Chris Theis Guest

    "ding feng" <> wrote in message
    news:...
    > 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
    Chris Theis, Jun 27, 2003
    #4
  5. ding feng

    Graham Cox Guest

    Re: how do I access a member of vector when this vector is a member of aclass

    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) == "no_match")
    {
    .....
    }
    else
    {
    .....
    }

    Something alonmg those lines - hope it helps!
    Graham Cox, Jun 27, 2003
    #5
  6. ding feng

    Graham Cox Guest

    Re: how do I access a member of vector when this vector is a member of aclass

    Graham Cox wrote:

    Whoops - small correction in last if statment!

    >
    > 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!
    Graham Cox, Jun 27, 2003
    #6
  7. ding feng

    Dhruv Guest


    > [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.


    >> 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<<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.
    Dhruv, Jun 27, 2003
    #7
  8. ding feng

    Chris Theis Guest

    "Dhruv" <> wrote in message
    news:p...
    >

    [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
    Chris Theis, Jun 28, 2003
    #8
  9. ding feng

    Dhruv Guest

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

    On Sat, 28 Jun 2003 17:57:55 +0200, Chris Theis wrote:

    >
    > "Dhruv" <> wrote in message
    > news:p...
    >>

    > [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.
    >
    >>
    >> 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.

    > 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.
    Dhruv, Jul 2, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,720
    Thomas Weidenfeller
    Oct 12, 2005
  2. Siemel Naran
    Replies:
    4
    Views:
    790
    Micah Cowan
    Jan 12, 2005
  3. pmatos
    Replies:
    6
    Views:
    23,739
  4. Replies:
    8
    Views:
    1,896
    Csaba
    Feb 18, 2006
  5. Replies:
    4
    Views:
    735
Loading...

Share This Page