How can I copy some range of element from a vector<string> to a string without an explicit loop?

Discussion in 'C++' started by Fred, Feb 7, 2007.

  1. Fred

    Fred Guest

    Hi:
    I've got the following request: (suppose the required head file
    is included)

    vector<string> vec;
    // ..........
    // push some items into vec

    string str; // here I want to
    copy some range of elements from vec to str, for example, from vec[2]
    to vec[5]

    copy( vec.begin()+2, vec.begin()+5,
    str.begin() ); // Fail

    string temp( vec.begin()+2,
    vec.begin()+5 ); // Construct a temporary string containing vec[2] to
    vec[5] and then copy temp to str, fail again

    So, is there anyway that without starting an explicit loop but
    achieve my goal? Thanks for help!
     
    Fred, Feb 7, 2007
    #1
    1. Advertising

  2. Re: How can I copy some range of element from a vector<string> toa string without an explicit loop?

    Fred wrote:
    > Hi:
    > I've got the following request: (suppose the required head file
    > is included)
    >
    > vector<string> vec;


    You mean :

    std::vector<char> vec;

    Right ?

    std::string( &vec[2], &vec[5]+1 );

    Should work.

    > // ..........
    > // push some items into vec
    >
    > string str; // here I want to
    > copy some range of elements from vec to str, for example, from vec[2]
    > to vec[5]
    >
    > copy( vec.begin()+2, vec.begin()+5,
    > str.begin() ); // Fail
    >
    > string temp( vec.begin()+2,
    > vec.begin()+5 ); // Construct a temporary string containing vec[2] to
    > vec[5] and then copy temp to str, fail again
    >
    > So, is there anyway that without starting an explicit loop but
    > achieve my goal? Thanks for help!
    >
     
    Gianni Mariani, Feb 7, 2007
    #2
    1. Advertising

  3. Fred

    Fred Guest

    On Feb 7, 4:33 pm, Gianni Mariani <> wrote:
    > Fred wrote:
    > > Hi:
    > > I've got the following request: (suppose the required head file
    > > is included)

    >
    > > vector<string> vec;

    >
    > You mean :
    >
    > std::vector<char> vec;
    >
    > Right ?


    In fact not, because vec are not necessarily a vector<char>.
    >
    > std::string( &vec[2], &vec[5]+1 );
    >
    > Should work.


    Agree.

    Thanks for answering, but my problem remains unresolved.
    >
    >
    >
    > > // ..........
    > > // push some items into vec

    >
    > > string str; // here I want to
    > > copy some range of elements from vec to str, for example, from vec[2]
    > > to vec[5]

    >
    > > copy( vec.begin()+2, vec.begin()+5,
    > > str.begin() ); // Fail

    >
    > > string temp( vec.begin()+2,
    > > vec.begin()+5 ); // Construct a temporary string containing vec[2] to
    > > vec[5] and then copy temp to str, fail again

    >
    > > So, is there anyway that without starting an explicit loop but
    > > achieve my goal? Thanks for help!- Hide quoted text -

    >
    > - Show quoted text -
     
    Fred, Feb 7, 2007
    #3
  4. Fred

    Kishore Yada Guest

    Fred wrote:
    > Hi:
    > I've got the following request: (suppose the required head file
    > is included)
    >
    > vector<string> vec;
    > // ..........
    > // push some items into vec
    >
    > string str; // here I want to
    > copy some range of elements from vec to str, for example, from vec[2]
    > to vec[5]
    >
    > copy( vec.begin()+2, vec.begin()+5,
    > str.begin() ); // Fail
    >
    > string temp( vec.begin()+2,
    > vec.begin()+5 ); // Construct a temporary string containing vec[2] to
    > vec[5] and then copy temp to str, fail again
    >
    > So, is there anyway that without starting an explicit loop but
    > achieve my goal? Thanks for help!


    I think accumulate should work.

    str = accumulate( vec.begin()+2, vec.begin()+5, string() );

    I could not test if the above code compiles as I do not have a C++
    compiler with me at present.
     
    Kishore Yada, Feb 7, 2007
    #4
  5. Fred

    Alan Johnson Guest

    Re: How can I copy some range of element from a vector<string> toa string without an explicit loop?

    Fred wrote:
    > On Feb 7, 4:33 pm, Gianni Mariani <> wrote:
    >> Fred wrote:
    >>> Hi:
    >>> I've got the following request: (suppose the required head file
    >>> is included)
    >>> vector<string> vec;

    >> You mean :
    >>
    >> std::vector<char> vec;
    >>
    >> Right ?

    >
    > In fact not, because vec are not necessarily a vector<char>.
    >> std::string( &vec[2], &vec[5]+1 );
    >>
    >> Should work.

    >
    > Agree.
    >
    > Thanks for answering, but my problem remains unresolved.
    >>
    >>
    >>> // ..........
    >>> // push some items into vec
    >>> string str; // here I want to
    >>> copy some range of elements from vec to str, for example, from vec[2]
    >>> to vec[5]
    >>> copy( vec.begin()+2, vec.begin()+5,
    >>> str.begin() ); // Fail
    >>> string temp( vec.begin()+2,
    >>> vec.begin()+5 ); // Construct a temporary string containing vec[2] to
    >>> vec[5] and then copy temp to str, fail again
    >>> So, is there anyway that without starting an explicit loop but
    >>> achieve my goal? Thanks for help!- Hide quoted text -

    >> - Show quoted text -

    >
    >


    It's hard to tell exactly what behavior you want. Is this it, maybe?

    #include <string>
    #include <sstream>
    #include <iterator>
    #include <algorithm>
    #include <vector>
    #include <iostream>

    using namespace std ;

    int main()
    {
    vector<string> strings ;
    strings.push_back("String0") ;
    strings.push_back("String1") ;
    strings.push_back("String2") ;
    strings.push_back("String3") ;
    strings.push_back("String4") ;
    strings.push_back("String5") ;
    strings.push_back("String6") ;

    stringstream ss ;
    copy(strings.begin() + 2, strings.begin() + 6,
    ostream_iterator<string>(ss, " ")) ;

    cout << ss.str() << endl ;
    }

    --
    Alan Johnson
     
    Alan Johnson, Feb 7, 2007
    #5
  6. Fred

    peter koch Guest

    On Feb 7, 9:24 am, "Fred" <> wrote:
    > Hi:
    > I've got the following request: (suppose the required head file
    > is included)
    >
    > vector<string> vec;
    > // ..........
    > // push some items into vec
    >
    > string str; // here I want to
    > copy some range of elements from vec to str, for example, from vec[2]
    > to vec[5]
    >
    > copy( vec.begin()+2, vec.begin()+5,
    > str.begin() ); // Fail
    >
    > string temp( vec.begin()+2,
    > vec.begin()+5 ); // Construct a temporary string containing vec[2] to
    > vec[5] and then copy temp to str, fail again
    >
    > So, is there anyway that without starting an explicit loop but
    > achieve my goal? Thanks for help!


    My approach would be to use a stringstream. Something like:

    std::vector<std::string> vec;
    std::stringstream ss;
    std::eek:stream_iterator<std::string> s(ss," ");
    std::string str;
    ...
    std::copy(vec.begin() + 2,vec.begin() +
    5,std::eek:stream_iterator<std::string>(ss," "));
    std::string str = ss.str();

    The advantage is that this allows a separator between the elements (if
    so desired) and that you can use any type of vector so long as its
    elements are streamable. You could create a (templated) function to
    simplify use.
    Kishore Yadas idea is also nice. It is simpler but restricted to
    containers of strings.

    /Peter
     
    peter koch, Feb 7, 2007
    #6
    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.

Share This Page