Results 1 to 11 of 11

Thread: Vector string extraction using cout?

  1. #1

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Vector string extraction using cout?

    I simply want to extract some values from a string vector, but the following code gives me errors
    #include<iostream.h>
    #include<vector>
    #include<string>
    using namespace std;

    void main(){
    vector<string> r;
    r.push_back("Hi");
    r.push_back("There");
    cout << r[0] << endl;
    cout << r[1] << endl;
    }

    errors:
    C:\Windows\vectorsample.cpp(10) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion
    )
    C:\Windows\vectorsample.cpp(11) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion
    )

    So I'm guessing that the << operand wont automatically extract a string vector(although it works with int,char,etc)....

    How do I do this? Do I need to overload the << operator, and if so, could someone explain to me how to do this, in code, or otherwise.

    Thank you...

  2. #2
    Zaei
    Guest
    Code:
    cout << r[0] << endl; 
    cout << r[1] << endl;
    should be
    Code:
    cout << r[0].c_str() << endl; 
    cout << r[1].c_str() << endl;
    If im not mistaken. That function returns a regular char*, that cout can work with.

    Z.

  3. #3

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Thanks

    Thanks, that worked..

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    *** don't they overload iostream operators?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Zaei
    Guest
    Laziness =).

    Z.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    bastards thats like one line of code
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Aah...but they did

    Check out istream_iterator and ostream_iterator. You can use those with copy to do things like this.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    ???

    so how would i use that in the above code?

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I just noticed the real problem.

    Use <iostream>, not <iostream.h>.

    The old one doesn't support strings.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You see, its crap like that which makes me doubt why this happens in C++ more than any language.

    Why didn't they just change the fricken name....

    But IT DID WORK NOW, albeit with 4 warnings about it, but not errors!

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It happens in C++ because it's a relatively new language. The standard was only frozen a couple of years ago, and many compilers still aren't up to speed on the newest feature, which is template-based streams. These are the new standard, and are under <iostream>. The old <iostream.h> shouldn't be used any more.

    PS: It doesn't do anything for you because it assumes you know what you're doing Don't expect C++ (or its parent C) to coddle you - it will let you trash the system.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width