简体   繁体   English

将数组字符串转换为char

[英]convert array string into char

i have taken all txt from file and place line by line into array of string. 我已经从文件中取出所有txt文件,并将它们逐行放置到字符串数组中。 I am trying to split this string so that i can save word by word in separate array. 我试图拆分此字符串,以便可以将单词逐个保存在单独的数组中。 Kindly just tell me how shell i convert the array of string into char. 请告诉我shell如何将字符串数组转换为char。

for example 例如

string line[15];  // line[0] has : was there before
                  // line[1] has : then after

char * pch;
char *c = line.c_str();   // string to char  (i am getting error here. Any body know?)
  pch = strtok (c," ");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ");
  }

error: C2228: left of '.c_str' must have class/struct/union 错误:C2228:“。c_str”的左侧必须具有class / struct / union

string line[15]; is an array of strings. 是一个字符串数组。 So when you have line.c_str(); 所以当你有line.c_str(); line is a pointer to a string and not a string itself. line是指向字符串的指针,而不是字符串本身。 A pointer doesn't have a .c_str() method and that's why the compiler is complaining. 指针没有.c_str()方法,这就是编译器抱怨的原因。 (Pointers don't have any methods and hence the compiler tells you that the left hand side of the expression must be a class/struct/union type). (指针没有任何方法,因此编译器告诉您表达式的左侧必须是类/结构/联合类型)。 To fix this you want to index into the array to get a string. 要解决此问题,您需要索引数组以获取字符串。 You can do this with something like: line[0].c_str(); 您可以使用以下方法执行此操作: line[0].c_str();

Additionally you can't write to anything returned by c_str() as it returns a const pointer. 另外,您无法写入c_str()返回的任何内容,因为它会返回const指针。 So you'll need to copy the results from c_str first before you then operate on it if you are going to change it in place. 因此,如果要更改它,则需要先从c_str复制结果,然后再对其进行操作。

Also it might be worth mentioning that there's c++ ways of doing tokenizing, you might find some examples here Split a string in C++? 同样值得一提的是,还有使用c ++进行标记化的方法,您可能会在这里找到一些示例在C ++中拆分字符串? . The last time I did this I was already using boost so I made use of the boost::tokenizer library. 上一次这样做时,我已经在使用boost,所以我使用了boost :: tokenizer库。

There are simpler ways to accomplish this in C++. 有一些更简单的方法可以在C ++中完成。 The strtok function is a C function and cannot be used with std::string directly since there is no way to get writable pointer access to the underlying characters in an std::string . strtok函数是C函数,不能直接与std::string一起使用,因为无法获取对std::string基础字符的可写指针访问。 You can use iostream s to get individual words separated by spaces from a file directly in C++. 您可以使用iostream来直接在C ++中从文件获取用空格分隔的单个单词。

Unfortunately the standard library lacks a simple, flexible, efficient method to split strings on arbitrary characters. 不幸的是,标准库缺乏一种简单,灵活,有效的方法来将字符串拆分为任意字符。 It gets more complicated if you want to use iostream s to accomplish splitting on something other than whitespace. 如果要使用iostream来完成除空白以外的内容的拆分,它将变得更加复杂。 Using boost::split or the boost::tokenizer suggestion from shuttle87 is a good option if you need something more flexible (and it may well be more efficient too). 如果需要更灵活的功能(可能也可能更有效),则使用shuttle87的boost::splitboost::tokenizer建议是一个不错的选择。

Here's a code example reading words from standard input, you can use pretty much the same code with an ifstream to read from a file or a stringstream to read from a string: http://ideone.com/fPpU4l 这是一个从标准输入中读取单词的代码示例,您可以将几乎相同的代码与ifstream一起使用来读取文件,或者将stringstream以读取字符串: http : //ideone.com/fPpU4l

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

using namespace std;

int main() {
    vector<string> words;
    copy(istream_iterator<string>{cin}, istream_iterator<string>{}, back_inserter(words));
    copy(begin(words), end(words), ostream_iterator<string>{cout, ", "});
    cout << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM