STL映射找到第一个不可重复的字符

时间:2022-09-13 09:01:18

I have written a small C++ program to keep a count of the alphabets. I am using stl map for the same,

我写了一个小的C ++程序来保持字母表的数量。我正在使用stl地图,

Interestingly, i am not getting the list as it appeared in the input. For example for the word TESTER, my program should give

有趣的是,我没有得到输入中出现的列表。例如对于TESTER这个词,我的程序应该给出

T  2
E  2
S  1
R  1

But its giving,

但它的给予,

E       2
R       1
S       1
T       2

change in the position of the alphabets,

改变字母的位置,

I want the o/p of the alphabets as it appeared in the input. Please help me if i am missing anything.Here is my code

我想要输入中出现的字母的o / p。如果我遗漏了什么,请帮助我。这是我的代码

#include<iostream>
#include<map>

using namespace std;

int main()
{
    char *str = "TESTER";
    map<char,int> checkmap;
    map<char,int>::iterator p;
    int i;
    while( *str != '\0' )
    {
        p = checkmap.find(*str);
        i = p->second;
        if(p == checkmap.end())
        {
            checkmap.insert(std::make_pair(*str,++i));
        }
        else
        {
            p->second = ++(p->second);
        }
    str++;
    }
    for(p=checkmap.begin(); p!=checkmap.end(); p++)
    {
        /*if(p->second == 1)
        {
            cout<<(*p).first<<endl;
        }*/
        cout<<p->first<<"\t"<<p->second<<endl;
    }

    return 0;
}

3 个解决方案

#1


5  

Here is shown an approach how it can be done

这里展示了如何完成它的方法

#include <iostream>
#include <map>
#include <cstring>

int main() 
{
    const char *str = "TESTER";

    auto order = [&]( char c1, char c2 )
    {
        return ( std::strchr( str, c1 ) < std::strchr( str, c2 ) );
    };

    std::map<char, int, decltype( order )> m( order );

    for ( const char *p = str; *p; ++p ) ++m[*p];

    for ( const auto &p : m ) std::cout << p.first << ' ' << p.second << std::endl;
    std::cout << std::endl;

    return 0;
}

The program output is

程序输出是

T 2
E 2
S 1
R 1

#2


1  

You're missing that std::map has its own internal ordering, which is completely independent of the order in which elements are added. As you can see from your example, it is ordered alphabetically. This is in increasing order of the value of the char key.

你错过了std :: map有自己的内部排序,它完全独立于添加元素的顺序。从您的示例中可以看出,它按字母顺序排序。这是char键值的递增顺序。

Also note that your map manipulations are overly complex. All you need to do is

另请注意,您的地图操作过于复杂。你需要做的就是

char *str = "TESTER";
map<char,int> checkmap;

while( *str != '\0' )
{
    checkmap[*str]++;
    ++str;
}

The while can be collapsed further if you're into that kind of thing:

如果你遇到这种情况,那么可能会进一步崩溃:

while( *str != '\0' ) checkmap[*str++]++;

For the general problem of mapping values while maintaining insertion order, see A std::map that keep track of the order of insertion?

有关在保持插入顺序的同时映射值的一般问题,请参阅跟踪插入顺序的std :: map?

#3


0  

There is no way to keep track of the order in which elements are added to map. To get the same order it would be advisable to use std::vector<std::char, int> and then update the same.

无法跟踪元素添加到地图的顺序。要获得相同的顺序,建议使用std :: vector 然后更新相同的顺序。

#1


5  

Here is shown an approach how it can be done

这里展示了如何完成它的方法

#include <iostream>
#include <map>
#include <cstring>

int main() 
{
    const char *str = "TESTER";

    auto order = [&]( char c1, char c2 )
    {
        return ( std::strchr( str, c1 ) < std::strchr( str, c2 ) );
    };

    std::map<char, int, decltype( order )> m( order );

    for ( const char *p = str; *p; ++p ) ++m[*p];

    for ( const auto &p : m ) std::cout << p.first << ' ' << p.second << std::endl;
    std::cout << std::endl;

    return 0;
}

The program output is

程序输出是

T 2
E 2
S 1
R 1

#2


1  

You're missing that std::map has its own internal ordering, which is completely independent of the order in which elements are added. As you can see from your example, it is ordered alphabetically. This is in increasing order of the value of the char key.

你错过了std :: map有自己的内部排序,它完全独立于添加元素的顺序。从您的示例中可以看出,它按字母顺序排序。这是char键值的递增顺序。

Also note that your map manipulations are overly complex. All you need to do is

另请注意,您的地图操作过于复杂。你需要做的就是

char *str = "TESTER";
map<char,int> checkmap;

while( *str != '\0' )
{
    checkmap[*str]++;
    ++str;
}

The while can be collapsed further if you're into that kind of thing:

如果你遇到这种情况,那么可能会进一步崩溃:

while( *str != '\0' ) checkmap[*str++]++;

For the general problem of mapping values while maintaining insertion order, see A std::map that keep track of the order of insertion?

有关在保持插入顺序的同时映射值的一般问题,请参阅跟踪插入顺序的std :: map?

#3


0  

There is no way to keep track of the order in which elements are added to map. To get the same order it would be advisable to use std::vector<std::char, int> and then update the same.

无法跟踪元素添加到地图的顺序。要获得相同的顺序,建议使用std :: vector 然后更新相同的顺序。