0

我创建了一组算法,它接受字符串向量的输入,检查是否有任何字符串出现不止一次:如果是,则从向量中删除所有额外出现的字符串,然后输出新的“较轻”数组裁员。

它很好用,除了现在我要让它不区分大小写;我试图简单地将toupper()std 函数添加到==比较语句中,但它似乎不起作用。

我对 Java 有更熟悉的背景,并且正在尝试学习 C++。
有人可以告诉我如何更正我的语法吗?

// Output old list.
cout << endl << "==========\nOld list:\n==========";
for (int i = 0; i < count; i++) {
    cout << endl << list[i];
}
cout << endl << endl;

// Check uniqueness.
for (int i = 0; i < count; i++)
    for (int j = i+1; j < count; j++) {
        if (toupper(list[i]) == toupper(list[j])) {
            list[j] = "";
            count--;
        }
    }

// Output new list.
cout << endl << "==========\nNew list:\n==========";
for (int i = 0; i < count; i++) {
    cout << endl << list[i];
}
cout << endl << endl;
4

5 回答 5

1

您的循环在数组向量中留下“洞” ,但数组向量的大小不会改变(但您会减小上限)list count

可能还有很多其他选择,但是如果您不想对其进行太多修改,则可能需要在附加循环中将数组中的非空元素复制list到新数组中

编辑:整合一些答案

首先,我们将有一个函数来执行 toUpper(这是从 @Jim22150 修改的)

std::string stringToUpper(const std::string &input) {
    std::string toBeModified=input;
    std::transform(toBeModified.begin(), toBeModified.end(), toBeModified.begin(), ::toupper);
    return toBeModified;
}

现在,我们不能留下漏洞,所以我们应该使用擦除(正如@Scott Christopher Stauffe 指出的那样):

// Output old list.
cout << endl << "==========\nOld list:\n==========";
for (int i = 0; i < count; i++) {
    cout << endl << list[i];
}
cout << endl << endl;

// Check uniqueness.
for (int i = 0; i < count; i++)
    for (int j = i + 1; j < count; j++) {
        if(stringToUpper(list[i]) == stringToUpper(list[j])) {
            list.erase(j,1);
            count--;
        }
    }
}

// Output new list.
cout << endl << "==========\nNew list:\n==========";
for (int i = 0; i < count; i++) {
    cout << endl << newlist[i];
}
cout << endl << endl;
于 2014-03-28T20:25:29.900 回答
1

我只是对 toupper 做了一个快速的谷歌搜索,但没有找到它的任何字符串版本。我见过的唯一标准 touppper() 是int toupper(int c);- 这意味着您只能使用它来比较单个字符!你试过stricmp()吗?

        if ( 0 == _stricmp(list[i], list[j]) ) {
            list[j] = "";
            count--;
        }

根据您的编译器,您可能拥有也可能不拥有此功能。

于 2014-03-28T20:14:50.593 回答
1

@DaveS,谢谢戴夫,我会试试的;它看起来干净而简短。但是,我发现使用变换并复制旧向量的方法更脏。

    // Output old list.
    cout << endl << "==========\nOld list:\n==========";
    for (int i = 0; i < count; i++) {
        cout << endl << list[i];
    }
    cout << endl << endl;

    // Check uniqueness.
    for (int i = 0; i < count; i++)
        for (int j = i + 1; j < count; j++) {
            std::transform(list[i].begin(), list[i].end(), list[i].begin(), ::toupper);
            std::transform(list[j].begin(), list[j].end(), list[j].begin(), ::toupper);
            if (list[i] == list[j]) {
                newlist[j] = "";
                count--;
            }
        }

    // Output new list.
    cout << endl << "==========\nNew list:\n==========";
    for (int i = 0; i < count; i++) {
        cout << endl << newlist[i];
    }
    cout << endl << endl;
于 2014-03-28T20:17:25.127 回答
1

如果您想像处理 Java 字符串一样轻松地处理 C++ 字符串,那么Boost 字符串算法库就是您的最佳选择。对于新手 C++ 程序员来说,安装 Boost 可能有点困难(尽管与许多其他 C++ 库相比它轻而易举),但它是有回报的。

您的问题基本上会简化为:

boost::algorithm::to_upper_copy(list[i]) == boost::algorithm::to_upper_copy(list[j])
于 2014-03-28T20:47:19.587 回答
0

首先,

list[j] = ""; // should never work. 

您可以使用擦除删除字符。

list.erase(j, 1);

或者,为了完全避免这种情况,您可以使用临时的“builder”字符串,并在需要时将其 push_back 字符。

于 2014-03-28T20:08:17.767 回答