How can I convert a string into a sequence of characters. I know that I can use char str[]="hello world" for example, however in my case my string is a changing variable. In my problem I read the first column from a text file and then store in a vector of strings, then I want to convert these strings into a sequence of characters. I have attached part of my code and I dont know how to proceed. As you can see I store the first column of my text file into a vector of strings. Now I want to change that vector of strings into a sequence of characters. Any I ideas how to do it. A little bit more information. The strings stored in a vector does not have the same length.
vector<string>cntnumber;
ifstream sengab;
sengab.open("/Users/ahmedsengab/Downloads/CNT_Location_200_200.txt");
if (!sengab){cout<<"unable to open file"<<endl;exit(1);}
int i=0;
while(!sengab.eof())
{
sengab>>id>>x>>y>>z;
cntnumber.push_back(id);
cout<<cntnumber[i]<<""<<endl;
i++;
}
}
/
A string IS a sequence of characters whether its a c-style string char mystring[] = "string";or a string object std::string someString; as defined in #include<string>
The variability of string length is much easier to handle and less error-prone using std::strings
No this is is not what I mean. I want to break each string into character variables. For example, If my string is "text" I would like to store as x='t',y='e', z='x' and k='t'; What you wrote actually reprints the string that is stored in cntnumber vector. I want to break each string in that vector and store it in a character vector. For example vector<char> x for the first character, vector<char> y for the second character, vector<char> z for the third character,
I want to break each string in that vector and store it in a character vector. For example vector<char> x for the first character, vector<char> y for the second character, vector<char> z for the third character,
Do you mean the char vector would contain the first letters of each of the strings, another vector would contain the second letters of each of the strings and so on? I'm not quite sure whether i understood.
No, I don't mean anybody's valuable contributions and I didn't read yours closely enough to say if its buggy or not. It will be interesting to see what happens with this large volume of info. Saturation perhaps. Who knows?
Thanks for the reply guys, but I think no one got what I meant. i Store my data in a string vector. I want to access the characters in the each string component of that vector. The problem with my string vector I can't use literals because my vector is large 2000 items. . I know how to access each string in my vector compoenent but I dont know how to convert it to an array of characters. If it is string literal i.e "" I can convert it easy but that is not the case. When I mentioned "text" it was an example. In my case my string vector has 2000 strings, they are not string literals. Lets say these are strings stored in my vector.
text
100009
kevin
nick.
etc.. till 2000 entries
Now how can you covert each string of these to characters so for examples
text -----would be 't','e','x','t'
10009 -----would be '1','0','0','0','9'
etc
Why do you need to convert a string to an array of characters?
By the way a string is basically an array of char. You can access each character of a string using array notation.
1 2 3 4
std:: string readFromFile = "text"; // Note you would replace "text" with the value read from your file.
std::cout << readFromFile[2] << std::endl; // Should print 'x'.
I need these characters to operate on them.
this is does not work for std:: string readfromfile=cntnumber;
it is not possible.
it has to be equal to a string literal i.e std:: string readfromfile="cntnumber";