简体   繁体   English

C ++ toupper()比较两个字符串

[英]c++ toupper() to compare two strings

I have created a set of algorithms that takes an input of a string vector, checks whether any of the strings occur more than once: if so erases all additional occurrences of the string from the vector, then outputs the new, 'lighter' array without the redundancies. 我创建了一组算法,该算法接受一个字符串向量的输入,检查是否有任何字符串出现多次:如果是,则从向量中删除该字符串中所有其他出现的字符串,然后输出新的“较轻”数组而不裁员。

It works great except now I am to make it case-insensitive; 它工作得很好,除了现在我要使其不区分大小写; I am attempting to simply add the toupper() std function to the == comparison statement, however it does not seem to work. 我试图将toupper() std函数简单地添加到==比较语句中,但是它似乎不起作用。

I have a more familiar background with Java and am trying to learn C++. 我对Java有更熟悉的背景,并且正在尝试学习C ++。
Can someone please show me how to correct my syntax? 有人可以告诉我如何更正我的语法吗?

// 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;

I just did a quick google of toupper and I didn't find any string versions of it. 我只是做了一个toupper的快速谷歌,但没有找到它的任何字符串版本。 The only standard touppper() I have seen is int toupper(int c); 我见过的唯一标准touppper()是int toupper(int c); - that means you can only use it to compare individual characters! -这意味着您只能使用它来比较各个字符! Have you tried stricmp() ? 您是否尝试过stricmp()

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

Depending on your compiler you may or may not have this function at your disposal. 根据您的编译器的不同,您是否可以使用此功能。

@DaveS, thanks Dave I will try that; @DaveS,谢谢戴夫,我会尽力的; it looks clean and short. 它看起来干净而短。 However, I found dirtier solution using transform and making a duplicate of the old vector. 但是,我发现使用变换并复制旧向量的方法更脏。

    // 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;

Your loop leaves "holes" in the list array vector, but the size of the array vector does not change (but you decrease your upper bound count ) 你的循环离开的“漏洞” list 阵列 载体,但 数组 向量的大小不会改变(但你减少上限count

There are probably many other alternatives, but if you don't want to modify it much, probably you need in an addtional loop to copy non-empty elements from the list array into a new array 可能还有许多其他选择,但是如果您不想对其进行太多修改,则可能需要在附加循环中将 非空元素从 list数组复制到新数组中

Edit: integrating some of the answers 编辑:整合一些答案

First we're going to have a function to do the toUpper (this is modified from @Jim22150) 首先,我们将有一个函数来执行toUpper(这是从@ Jim22150修改而来的)

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

Now, we must not leave holes, so we should use erase (as @Scott Christopher Stauffe indicated): 现在,我们一定不能留下漏洞,所以我们应该使用擦除(如@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;

First of all, 首先,

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

You can remove a char by using erase. 您可以使用擦除来删除字符。

list.erase(j, 1);

Alternatively, to avoid this altogether, you could use a temporary "builder" string and just push_back chars to it when needed. 或者,为完全避免这种情况,您可以使用临时的“ builder”字符串,并在需要时仅将push_back字符分配给它。

If you want to handle C++ strings as easily as Java strings, then the Boost String Algorithms Library is the way to go. 如果您想像处理Java字符串一样轻松地处理C ++字符串,那么Boost String Algorithms库就是您的最佳选择。 Installing Boost may be a bit hard for a newbie C++ programmer (although it's a breeze compared to many other C++ libraries), but it pays off. 对于新手C ++程序员来说,安装Boost可能会有些困难(尽管与许多其他C ++库相比,这是轻而易举的事),但确实有回报。

Your problem will essentially be reduced to this: 您的问题基本上可以归结为:

boost::algorithm::to_upper_copy(list[i]) == boost::algorithm::to_upper_copy(list[j])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM