how to store a string into a vector?

Discussion in 'C++' started by vijetha, Jan 1, 2006.

  1. vijetha

    vijetha Guest

    Hi,
    I have been trying to store a string into a vector but I am not
    able to figure out how to do it. I am able to use vectors for storing
    integers but not able to use them fro strings. I want to be able to
    convert vector to string and vice versa.
    Cheers,
    Vijetha
     
    vijetha, Jan 1, 2006
    #1
    1. Advertising

  2. vijetha wrote:
    > Hi,
    > I have been trying to store a string into a vector but I am not
    > able to figure out how to do it. I am able to use vectors for storing
    > integers but not able to use them fro strings. I want to be able to
    > convert vector to string and vice versa.
    > Cheers,
    > Vijetha
    >

    Guessing about what you mean:

    #include <string>
    #include <vector>

    int main(void) {
    std::vector<std::string> vestring;
    vestring.push_back("A String");
    }

    Greetings,
    --
    Stephan 'hagbard' Grein, <>
    http://hagbard.ninth-art.de/
    GnuPG-Key-ID: 0x08FA3507
    <ESC> :wq
     
    Stephan Grein, Jan 1, 2006
    #2
    1. Advertising

  3. vijetha

    Daniel T. Guest

    In article <>,
    "vijetha" <> wrote:

    > Hi,
    > I have been trying to store a string into a vector but I am not
    > able to figure out how to do it. I am able to use vectors for storing
    > integers but not able to use them fro strings. I want to be able to
    > convert vector to string and vice versa.
    > Cheers,
    > Vijetha


    Do you mean something like this?

    #include <string>
    #include <vector>

    int main() {
    using namespace std;
    string s( "hello world" );

    vector<char> v( s.begin(), s.end() );

    assert( v.size() == 11 );
    assert( v[0] == 'h' );
    assert( v[10] == 'd' );
    }


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
     
    Daniel T., Jan 1, 2006
    #3
  4. vijetha

    Luke Meyers Guest

    Daniel T. wrote:
    > Do you mean something like this?
    >
    > #include <string>
    > #include <vector>
    >
    > int main() {
    > using namespace std;
    > string s( "hello world" );
    >
    > vector<char> v( s.begin(), s.end() );
    >
    > assert( v.size() == 11 );
    > assert( v[0] == 'h' );
    > assert( v[10] == 'd' );
    > }


    I certainly hope not... why would someone want to do this?

    Luke
     
    Luke Meyers, Jan 2, 2006
    #4
  5. vijetha

    Guest

    Luke Meyers wrote:
    > Daniel T. wrote:
    > > Do you mean something like this?
    > >
    > > #include <string>
    > > #include <vector>
    > >
    > > int main() {
    > > using namespace std;
    > > string s( "hello world" );
    > >
    > > vector<char> v( s.begin(), s.end() );
    > >
    > > assert( v.size() == 11 );
    > > assert( v[0] == 'h' );
    > > assert( v[10] == 'd' );
    > > }

    >
    > I certainly hope not... why would someone want to do this?


    Because he has an API doUpperCase(char*), but s.c_str() returns only a
    char const*? &v[0] is a proper char*.

    HTH,
    Michiel Salters
     
    , Jan 2, 2006
    #5
  6. vijetha

    James Juno Guest

    wrote:
    > Luke Meyers wrote:
    >> Daniel T. wrote:
    >>> Do you mean something like this?
    >>>
    >>> #include <string>
    >>> #include <vector>
    >>>
    >>> int main() {
    >>> using namespace std;
    >>> string s( "hello world" );
    >>>
    >>> vector<char> v( s.begin(), s.end() );
    >>>
    >>> assert( v.size() == 11 );
    >>> assert( v[0] == 'h' );
    >>> assert( v[10] == 'd' );
    >>> }

    >> I certainly hope not... why would someone want to do this?

    >
    > Because he has an API doUpperCase(char*), but s.c_str() returns only a
    > char const*? &v[0] is a proper char*.
    >
    > HTH,
    > Michiel Salters
    >


    This is still an abomination. He's better off using string::pointer if
    he really wants to get to that buffer.

    --JJ
     
    James Juno, Jan 2, 2006
    #6
  7. vijetha

    Gavin Deane Guest

    James Juno wrote:

    > wrote:
    > > Luke Meyers wrote:
    > >> Daniel T. wrote:
    > >>> Do you mean something like this?
    > >>>
    > >>> #include <string>
    > >>> #include <vector>
    > >>>
    > >>> int main() {
    > >>> using namespace std;
    > >>> string s( "hello world" );
    > >>>
    > >>> vector<char> v( s.begin(), s.end() );
    > >>>
    > >>> assert( v.size() == 11 );
    > >>> assert( v[0] == 'h' );
    > >>> assert( v[10] == 'd' );
    > >>> }
    > >> I certainly hope not... why would someone want to do this?

    > >
    > > Because he has an API doUpperCase(char*), but s.c_str() returns only a
    > > char const*? &v[0] is a proper char*.
    > >
    > > HTH,
    > > Michiel Salters
    > >

    >
    > This is still an abomination. He's better off using string::pointer if
    > he really wants to get to that buffer.


    string::pointer is a typedef. How does that help?

    Gavin Deane
     
    Gavin Deane, Jan 2, 2006
    #7
  8. vijetha

    James Juno Guest

    Gavin Deane wrote:
    > James Juno wrote:
    >
    >> wrote:
    >>> Luke Meyers wrote:
    >>>> Daniel T. wrote:
    >>>>> Do you mean something like this?
    >>>>>
    >>>>> #include <string>
    >>>>> #include <vector>
    >>>>>
    >>>>> int main() {
    >>>>> using namespace std;
    >>>>> string s( "hello world" );
    >>>>>
    >>>>> vector<char> v( s.begin(), s.end() );
    >>>>>
    >>>>> assert( v.size() == 11 );
    >>>>> assert( v[0] == 'h' );
    >>>>> assert( v[10] == 'd' );
    >>>>> }
    >>>> I certainly hope not... why would someone want to do this?
    >>> Because he has an API doUpperCase(char*), but s.c_str() returns only a
    >>> char const*? &v[0] is a proper char*.
    >>>
    >>> HTH,
    >>> Michiel Salters
    >>>

    >> This is still an abomination. He's better off using string::pointer if
    >> he really wants to get to that buffer.

    >
    > string::pointer is a typedef. How does that help?
    >
    > Gavin Deane
    >


    Point taken, but the whole thing is ugly from a readability stand-point
    and in this case, I hope whatever location he passes to the API function
    doesn't affect the length of the array. Granted, my solution doesn't
    help in that case either. Thankfully we can do something like:

    transform(str.begin(), str.end(), str.begin(), toupper);

    or some other such function-based manipulation.

    -JJ
     
    James Juno, Jan 2, 2006
    #8
  9. vijetha

    Luke Meyers Guest

    James Juno wrote:
    > transform(str.begin(), str.end(), str.begin(), toupper);


    Now you're cookin' with gas.

    Keep in mind that toupper is in the global namespace, though, so you'll
    have to either use qualifiers or using-decls for the std stuff, or
    qualify it as ::toupper. The following compiles and works:

    #include <string>
    #include <algorithm>
    #include <cctype>
    #include <iostream>

    int main() {
    using namespace std;
    string s1 = "hello, world!";

    string::iterator begin = s1.begin();
    string::iterator end = s1.end();
    transform(begin, end, begin, ::toupper);

    cout << s1 << endl;

    return EXIT_SUCCESS;
    }

    Luke
     
    Luke Meyers, Jan 3, 2006
    #9
  10. vijetha

    Gavin Deane Guest

    Luke Meyers wrote:

    > James Juno wrote:
    > > transform(str.begin(), str.end(), str.begin(), toupper);

    >
    > Now you're cookin' with gas.
    >
    > Keep in mind that toupper is in the global namespace, though, so you'll
    > have to either use qualifiers or using-decls for the std stuff, or
    > qualify it as ::toupper. The following compiles and works:


    >From 17.4.1.2/4


    [...] the contents of each header cname shall be the same as that of
    the corresponding header name.h [...]. In the C++ Standard Library,
    however, the declarations and definitions (except for names which are
    defined as macros in C) are within namespace scope of the namespace
    std.

    > #include <string>
    > #include <algorithm>
    > #include <cctype>
    > #include <iostream>
    >
    > int main() {
    > using namespace std;
    > string s1 = "hello, world!";
    >
    > string::iterator begin = s1.begin();
    > string::iterator end = s1.end();
    > transform(begin, end, begin, ::toupper);
    >
    > cout << s1 << endl;
    >
    > return EXIT_SUCCESS;
    > }


    So <cctype> puts toupper in the std namespace only. The above code is
    therefore incorrect. The fact that it compiles on almost every compiler
    out there is enough for me to prefer <name.h> to <cname> headers.
    Deprecated maybe, but it's correct.

    Gavin Deane
     
    Gavin Deane, Jan 3, 2006
    #10
    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. pmatos
    Replies:
    6
    Views:
    23,852
  2. Replies:
    8
    Views:
    1,939
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    574
    James Kanze
    Sep 4, 2007
  4. ssylee
    Replies:
    1
    Views:
    1,217
    Zachary Turner
    Jan 13, 2009
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    368
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page