如何在C ++中将单词排序为结构数组?

时间:2014-03-27 22:19:43

标签: c++ arrays string structure

我对C ++很陌生,所以对我可能会或可能不会理解的词语轻松一点...... 我们给出了一个字符串形式的段落,我们应该从字符串中获取每个单词并填充一个数组,并显示该数组。我们今天了解了结构,这就是为什么我们应该输出docWordCount(以及为什么有些东西可能出错......)

我要做的是沿着字符串移动直到我找到一个空格,当我这样做时,我使用.substr命令将单词复制到数组中。我最初尝试使用static_cast来查找是否有空格,我不确定问题是它是不是那样工作,或者我做错了什么(可能是后者)。每当我沿着字符串移动时,我会将字数增加1,所以它会输出所有字而不是它前面的字。另外我应该提一下,当我编译代码时,它输出文本然后它给了我一个" Debug断言失败了! [...]表达式:字符串下标超出范围。"另一个窗口出错。

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

int main()
{

    struct wordCount
    {
        string word;
        int count;
    };

    wordCount docWordCount [500];   
    for(int i = 0; i < 500; i++)
    {
        docWordCount[i].word = "";
        docWordCount[i].count = 0;
    }

string text ="If there's one good thing that's come out of the European debt crisis\
        it's that the U.S. has been able to shield itself from much of \
            the mess. The sentiment will continue in 2012 as the U.S.  \
            economy is expected to grow faster than its European counterparts. \
        That's according to the Organization for Economic Cooperation and \
        Development which says the U.S. economy will expand at a 2.9% \
            annual rate in the first quarter and then a 2.8% rate in the second quarter.";
cout << text << endl;

int wordLength = 0;

for(int i = 0; i < 500; i++)
    {
    if (text[i] == ' ' ) //if there isnt a space
        wordLength++;

    if (text[i] == !' ' ) //if there is a space
        docWordCount[i].word = text.substr(i - wordLength, i);
    }

for (int i = 0; i < 100; i++)
cout << docWordCount[i].word << endl;
    return 0;

}

应该是什么样的

If
Theres
one
good
thing
等等......我正试图做声音吗?有没有更简单的方法来解决这个问题?

2 个答案:

答案 0 :(得分:0)

一些错误,但另有声音。

  1. 您将索引与text混淆,并将索引放入单词数组中;为后者创建一个新变量wordIndex
  2. 检查空间的操作员是错误的。 ==表示 ,使用!= 不是
  3. 您应该为i
  4. 重复text.length()
  5. 最好将500硬代码更改为常量。
  6. 重要的是,你想要text.substr(i - wordLength, wordLength);即第二个参数是长度,而不是子串的结束索引。

答案 1 :(得分:0)

您的代码包含多个错误。例如这句话

if (text[i] == !' ' ) //if there is a space

无效。它将字符text[i]与布尔值false(!' '

进行比较

尝试以下代码(不进行测试)。也许它包含的错误比你的代码少。:)

#include <string>
#include <iostream>

int main()
{
    struct wordCount
    {
        std::string word;
        int count;
    };

    const size_t N = 500;
    wordCount docWordCount[N] = {};   

    std::string text ="If there's one good thing that's come out of the European debt crisis\
                       it's that the U.S. has been able to shield itself from much of \
                       the mess. The sentiment will continue in 2012 as the U.S.  \
                       economy is expected to grow faster than its European counterparts. \
                       That's according to the Organization for Economic Cooperation and \
                       Development which says the U.S. economy will expand at a 2.9% \
                       annual rate in the first quarter and then a 2.8% rate in the second quarter.";

    std::cout << text << std::endl;

    size_t n = 0;
    std::string::size_type i = 0;

    while ( n < N && i < text.size() )
    {
        while ( i < text.size() && text[i] == ' ' ) ++i;

        std::string::size_type j = i;
        while ( i < text.size() && text[i] != ' ' ) ++i;

        if ( j != i ) docWordCount[n++].word.assign( text, j, i - j );
    }


    for ( size_t i = 0; i < n; i++ ) std::cout << docWordCount[i].word << std::endl;

    return 0;
}

如果您使用类std :: string的成员函数,例如find

,那会更简单

顺便说一下,线程的名称与其描述不符。没有关于排序的说法。