【发布时间】:2014-03-28 19:59:46
【问题描述】:
我创建了一组算法,它接受字符串向量的输入,检查是否有任何字符串出现多次:如果是,则从向量中删除所有额外出现的字符串,然后输出新的,'lighter ' 没有冗余的数组。
它很好用,但现在我要让它不区分大小写;我试图简单地将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;
【问题讨论】:
-
为什么不将
std::vector<std::string>与std::remove_if和lambda一起使用? -
@OMGtechy 不区分大小写吗?
-
@Jim22150 列表中字符串的顺序需要维护吗?
-
@Mahesh,是的,输出相同的顺序,我想保持我的算法相同。这不是问题的意义所在(对 OMGtechy 和 Scott)。
-
怎么不工作了?什么是清单? std::string[N]?