C++禁止从const char*设置char*和char []

Jos*_*osh 1 c++ const char

这是代码.我不知道它为什么不承认它需要复制内存,我不能强迫它.

 string message="The quick brown fox jumped over the lazy dog.";

  vector<char*> words;

  while(message.length()>0){
    char wtf[message.substr(0,message.find(" ")).c_str().length()]=message.substr(0,message.find(" ")).c_str();
    words.push_back(wtf);
    message=message.substr(message.find(" ")+1);
  }
Run Code Online (Sandbox Code Playgroud)

我看到有类似的线程,但没有.而且,C++似乎无法轻易解决这个问题.

Cat*_*lus 6

您希望按空格将字符串拆分为标记.你应该使用一个合适的工具 - 例如Boost.Tokenizer.您的代码在几个方面有误:

  1. 您无法定义这样的数组,维度必须是编译时常量表达式.
  2. 数组不是指针,也不能分配给数组.
  3. c_str 只要字符串对象有效,就返回一个有效的指针.
  4. char*除非你需要,否则不要使用.使该向量保持不变std::string.
  5. c_str返回一个char*没有length成员函数的函数,或者该函数的任何成员函数.

这表明你在C++中缺乏一些基本的知识.你应该读一本关于C++的好书.所以,不,这不是C++的缺点.

真的,只需使用Boost.Tokenizer.它有一个按文档中的空格分割的示例.


Mar*_*ork 6

如何将文本分解为单词(简单方法)

#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
    std::string message="The quick brown fox jumped over the lazy dog.";

    std::vector<std::string>    words;

    std::stringstream   stream(message);                  // 1: Create a stream
    std::copy(std::istream_iterator<std::string>(stream), // 2: Copy words from the stream
              std::istream_iterator<std::string>(),
              std::back_inserter(words));                 //    into the back of the vector.
}
Run Code Online (Sandbox Code Playgroud)

分解它是如何工作的(对于12岁的学习计划)

  • 运算符>>应用于流(和字符串)时读取单个(白色)空格分隔的单词.

    std::string message="The quick brown fox jumped over the lazy dog.";
    std::stringstream   stream(message);
    
    std::string  word;
    stream >> word; // Reads "The"
    stream >> word; // Reads "quick"
    stream >> word; // Reads "brown" etc...
    
    Run Code Online (Sandbox Code Playgroud)
  • istream_iterator是流的适配器,使它们看起来像容器.
    它使用运算符>>从"T"类型的流中读取项目

    std::stringstream   stream("The quick brown fox jumped over the lazy dog.");
    std::istream_iterator<std::string> i(stream);
    
    std::string  word;
    
    word = *i;      // de-reference the iterator to get the object.   Reads "The"
    ++i;
    word = *i; ++i; // Reads "quick"
    word = *i; ++i; // Reads "brown" etc
    
    // Works for any type that uses >> to read from the stream 
    std::stringstream   intstream("99 40 32 64 20 10 9 8 102");
    std::istream_iterator<int> i(stream);  // Notice the type is int here
    
    int   number;
    
    number = *i;      // de-reference the iterator to get the object.   Reads "99"
    ++i;
    number = *i; ++i; // Reads "44"
    number = *i; ++i; // Reads "32" etc
    
    Run Code Online (Sandbox Code Playgroud)
  • 标准算法都适用于迭代器.
    std :: copy遍历源并将每个项目放在目标中:

    int    src[] = { 1, 2, 3, 4, 5, 6 };
    int    dst[] = { 0, 0, 0, 0, 0, 0 };
    
    std::copy(src, src+6, dst); // copies src into dst
                                // It assumes there is enough space in dst
    
    Run Code Online (Sandbox Code Playgroud)
  • back_inserter是一个使用push_back将项添加到容器的适配器.
    我们可以确保目标向量的大小正确.但是使用back_inserter更容易确保向量是动态调整大小的.

    int    src[] = { 1, 2, 3, 4, 5, 6 };
    std::vector<int> dst; // Currently has zero size
    
    std::copy(src, src+6, std::back_inserter(dst)); // copies src into dst
                                                    // back_inserter expands dst to fit
                                                    // by using push_back
    
    Run Code Online (Sandbox Code Playgroud)
  • 将它们重新组合在一起:

    // Create a stream from the string
    std::stringstream   stream(message);
    
    // Use std::copy to copy from the string.
    //     The stream is iterated over a word at a time.
    //     because the istream iterator is using std::string type.
    //
    //     The istream_iterator with no parameters is equivelent to end.
    //
    //     The destination appends the word to the vector words.
    std::copy(std::istream_iterator<std::string>(stream), // 2: Copy words from the stream
              std::istream_iterator<std::string>(),
              std::back_inserter(words));                 //    into the back of the vector.
    
    Run Code Online (Sandbox Code Playgroud)