|
-
Dec 10th, 2001, 08:44 PM
#1
Thread Starter
I wonder how many charact
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...
-
Dec 10th, 2001, 09:04 PM
#2
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.
-
Dec 10th, 2001, 09:22 PM
#3
Thread Starter
I wonder how many charact
-
Dec 10th, 2001, 09:23 PM
#4
transcendental analytic
*** 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.
-
Dec 10th, 2001, 10:05 PM
#5
-
Dec 10th, 2001, 10:25 PM
#6
transcendental analytic
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.
-
Dec 11th, 2001, 03:13 PM
#7
Monday Morning Lunatic
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
-
Dec 11th, 2001, 11:31 PM
#8
Thread Starter
I wonder how many charact
???
so how would i use that in the above code?
-
Dec 13th, 2001, 07:54 AM
#9
Monday Morning Lunatic
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
-
Dec 13th, 2001, 09:32 AM
#10
Thread Starter
I wonder how many charact
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!
-
Dec 13th, 2001, 11:42 AM
#11
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|