Mar 1, 2010 at 2:16am UTC
Hi to all,
here's what I'm faced with.
I know that one can do:
char c='b';
int i=c;
and that would give the ascii code of the letter b. So far so good.
Now, this is what I have. I define the function:
vector<string> my_vector(vector<string> my_vector2)
{
for(int i = 0; i < my_vector2.size(); i++)
{
int c = my_vector2[i];
//more stuff here
}
//more stuff here
}
What's wrong with the line in bold? How would I access the ascii code of a specific character in the vector?
Thanks!
Mar 1, 2010 at 2:44am UTC
chrisname thanks! Actually this is what I've been trying
int c = my_vector2.at(i);
Also
int c = (int)my_vector2.at(i);
Also
int c = (int)(my_vector2.at(i));
but none seems to work.
All I wanted is get the ascii code of my_vector2 at the ith position.
Mar 1, 2010 at 2:51am UTC
Well, what problem do you get when you use at or operator[]?
Edit: do you get a compile-time error or a runtime error? If so, what happens?
Last edited on Mar 1, 2010 at 2:52am UTC
Mar 1, 2010 at 3:03am UTC
when I use
int c = (int)my_vector2.at(i);
I get a compile-time error
19 C:\Dev-Cpp\main.cpp `struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' used where a `int' was expected
Mar 1, 2010 at 3:10am UTC
Oh, so he does :P
I should learn to read.
Mar 1, 2010 at 3:27am UTC
cool. And so say that I am using a vector of characters how do I pass a vector to the function?
After the function has been defined can I simply say
int main(int argc, char* argv[])
{
cout << my_vector ('Hello World')<< endl;
return 0;
}
Is this is how I pass a vector of characters into the function? Put that vector of characters (in the case the sentence Hello World) into quotes?
Because when I tried to call the function this way at compilation I got
54:29 C:\Dev-Cpp\main.cpp [Warning] character constant too long for its type
and
54 C:\Dev-Cpp\main.cpp conversion from `int' to non-scalar type `std::vector<char, std::allocator<char> >' requested