注册 登录
编程论坛 C++教室

[求助]字符串怎样转化为字符数组

Powerqy 发布于 2007-04-10 22:25, 1580 次点击
vector里面是一行一行的字符串,现在要一行一行读出来转化成字符数组,该怎么解决?
6 回复
#2
I喜欢c2007-04-10 23:19
赋给字符数组就是..
#3
wfpb2007-04-11 09:19
vector<string>中的元素换成char[] ???
vecstr.at(0).c_str()就是C风格的字符串了
#4
Powerqy2007-04-11 09:58


#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

int main() {
vector<string> v;
ifstream in ("ttt.txt");
string line;

while (getline(in, line)) {
v.push_back(line);
}

char str[] = v[0].c_str; 这一行会报错cannot convert from '' to 'char []'

cin.get();
}

也试过用char str = v[0].get();把他放在循环里,但是还是有错'get' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
请大家帮忙给看看

#5
游乐园2007-04-11 12:04
以下是引用Powerqy在2007-4-11 9:58:31的发言:


#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

int main() {
vector<string> v;
ifstream in ("ttt.txt");
string line;

while (getline(in, line)) {
v.push_back(line);
}

const char *str = v[0].c_str(); //C++中把字符数组按字符串常量对待的 所以注意const

cin.get();
}

也试过用char str = v[0].get();把他放在循环里,但是还是有错'get' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
请大家帮忙给看看

#6
Powerqy2007-04-11 12:46

谢谢你

#7
wfpb2007-04-21 22:37

最好用at(i)而不用下标访问,因为at里面有断言,帮助检查错误.

1