【问题标题】:String Char Iterator字符串字符迭代器
【发布时间】:2012-10-23 19:49:05
【问题描述】:

刚刚回到 C++ 编程。 我得到的错误:

在发送的成员开始的请求是非类类型 char[30]

send 中对成员端的请求是非类类型 char[30]

char sent[] = "need to break this shiat down";
    for(vector<string>::iterator it=sent.begin(); it!=sent.end(); ++it){
        if(*it == " ")
            cout << "\n";
        else
            cout << *it << endl;
    }

我应该将字符更改为字符串还是以不同的方式定义向量?

【问题讨论】:

  • 错误的 var 声明,应该是 char[] sent = "need to break this shiat down"
  • @imulsion 声明是正确的。

标签: c++ iterator


【解决方案1】:

您的变量sent 不是vector&lt;string&gt; 类型,而是char[]

但是,您的 for 循环会尝试遍历 字符串向量

对于普通数组,使用 C 迭代:

 int len = strlen(sent);
 for (int i = 0; i < len; i++)

【讨论】:

    【解决方案2】:

    使用string 而不是char[]

    string sent = "need to break this shiat down";
    for(string::iterator it=sent.begin(); it!=sent.end(); ++it){
        if(*it == ' ')
            cout << "\n";
        else
            cout << *it << endl;
    }
    

    char[] 没有开始和结束方法..

    【讨论】:

      【解决方案3】:

      在其他答案中已指出您正在迭代错误的类型。您应该将sent 定义为std::string 类型并使用std::string::begin()std::string::end() 进行迭代,或者,如果您有C++11 支持,您有一些选项可以轻松地迭代固定大小的数组。您可以使用 std::begin 和 std::end` 进行迭代:

      char sent[] = "need to break this shiat down";
      for(char* it = std::begin(sent); it != std::end(sent); ++it){
          if(*it == ' ')
              std::cout << "\n";
          else
              std::cout << *it << "\n";
      }
      

      或者您可以使用基于范围的循环:

      char sent[] = "need to break this shiat down";
      for (const auto& c : sent)
      {
        std::cout << c << "\n";
      }
      

      【讨论】:

        【解决方案4】:

        char sent[] 不是 std::string 而是字符串文字 - 但在这种情况下,您可以对其进行迭代:

        int main() {
        char sent[] = "need to break this shiat down";
            for(auto it = std::begin(sent); it!=std::end(sent) - 1; ++it){
                if(*it == ' ')
                    cout << "\n";
                else
                    cout << *it << endl;
            }
        }
        

        请注意,我将 " " 更改为 ' ' - 并跳过了最后一个 null 终止字符 '\0'...

        现场示例:http://liveworkspace.org/code/55f826dfcf1903329c0f6f4e40682a12

        对于 C++03,您可以使用这种方法:

        int main() {
        char sent[] = "need to break this shiat down";
            for(char* it = sent; it!=sent+sizeof(sent) - 1; ++it){
                if(*it == ' ')
                    cout << "\n";
                else
                    cout << *it << endl;
            }
        }
        

        如果这是此时未知大小的字符串文字 - 使用 strlen 而不是 sizeof...

        【讨论】:

          【解决方案5】:

          您还可以使用流式传输来去掉空格并加入换行符。

          #include <iostream>
          #include <sstream>
          #include <string>
          using namespace std;
          
          int main(int argc, char *argv[])
          {
              stringstream ss("need to break this shiat down.", ios_base::in);
          
              string s;
              while (ss >> s)
                  cout << s << endl;
          
              return EXIT_SUCCESS;
          }
          

          结果:

          需要

          休息
          这个
          希特
          下来。

          【讨论】:

          • 看起来是在“”(空格)上拆分的最简单解决方案。
          猜你喜欢
          • 2011-07-22
          • 2011-06-03
          • 2014-04-21
          • 2012-02-24
          • 2012-08-19
          • 1970-01-01
          • 2020-12-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多