【问题标题】:getting bus error : 10 with string append获取总线错误:10 带有字符串附加
【发布时间】:2014-02-21 08:22:15
【问题描述】:

我有一个函数,它接受两个字符串并确定它们是否相同。我正在尝试对字符串进行标记并将所有标记组合成一个字符串。这是我目前所拥有的,我收到了总线错误:10。
任何帮助表示赞赏。

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

    bool stringCheck(string s1, string s2){
    string strCheck1 = "";
    string strCheck2 = "";

    char *cstr1 = new char[s1.length()]; // char array with length of string
    strcpy(cstr1, s1.c_str());  // copies characters of string to char array

    char *cstr2 = new char[s2.length()];
    strcpy(cstr2, s2.c_str());

    char *p1 = strtok(cstr1, " ");  // creates a char array that stores token that 
                                    // is delimeted 
    cout << "p1 " << p1 << endl;    ///outputs token that is found

    strCheck1.append(p1);                       // appends token to string 
    cout << "strCheck1  " << strCheck1 << endl; // outputs string

    while(p1 != NULL)               // while the token is not a null character
    {
        cout<<"parsing" << endl;    
        p1 = strtok(NULL, " ");     // continue to parse current string.  
        cout << "p1 " << p1 << endl; 
        strCheck1.append(p1);
        cout << "str1  " << strCheck1 << endl;
    }

    char * p2 = strtok(cstr2, " ");
    cout << "p2 " << p2 << endl; 
    strCheck2.append(p2);
    cout << "strCheck2  " << strCheck2 << endl;

    while(p2 != null){
        p2 = strtok(NULL, " ");
        strCheck2.append(p2);
        cout << "str2  " << strCheck2 << endl;
    }

    if( strCheck1.compare(strCheck2) != 0)
    {
        return 0;
    }
    else return 1;
}

int main(void){
    string s1 = "jam yoooo jay";
    string s2 = "jam    yoooo";
    if(stringCheck(s1, s2) == 1){
        cout << "strings same"<< endl;;
    }
    else{
        cout << "strings not same" << endl;
    }

}

有没有我可以配对的条件语句

while(p1 != NULL)

我知道这是一个非常愚蠢的功能,但只是想提高我的技能。任何帮助表示赞赏!

【问题讨论】:

    标签: c++ append strtok


    【解决方案1】:

    有些事情你必须改变:

    • char *cstr1 = new char[s1.length()];

      c-string 是以 null 结尾的,所以你需要一个 char 来存储 null 字符:

      char *cstr1 = new char[s1.length() + 1];

      cstr2 相同)

    • strCheck1.append(p1)

      p1 不能是空指针(有关详细信息,请参阅Assign a nullptr to a std::string is safe?)。所以你必须检查......

      if (p1) strCheck1.append(p1);

      p2 相同)。

    • cout &lt;&lt; p1 &lt;&lt; endl

      如果p1 是空指针,则可能会发生坏事(请参阅Why does std::cout output disappear completely after NULL is sent to it)。所以你必须检查......

      if (p1) { cout &lt;&lt; "p1 " &lt;&lt; p1 &lt;&lt; endl; strCheck1.append(p1); }

      p2 相同)

    • 存在内存泄漏(cstr1 / cstr2 必须删除)。

    最后它应该可以工作了。

    您可能应该考虑使用其他系统来提取令牌(您不必混合 std::string 和 c-string)。例如:

    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
      std::string text("text-to-tokenize");
      std::istringstream iss(text);
      std::string token;
    
      while(getline(iss, token, '-'))
        std::cout << token << std::endl;
    
      return 0;
    }
    

    【讨论】:

    • 非常感谢。听起来真的很有帮助。我会再试一次!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2015-04-25
    • 1970-01-01
    • 2023-04-02
    • 2023-03-05
    • 2017-07-06
    相关资源
    最近更新 更多