Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何让char[]使用std::map_C++_Arrays_Stl_Map - Fatal编程技术网

C++ 如何让char[]使用std::map

C++ 如何让char[]使用std::map,c++,arrays,stl,map,C++,Arrays,Stl,Map,回答后编辑: 您需要实现您需要首先实现第一件事:永远不要使用get。它无法安全使用,任何使用它的程序都存在安全漏洞。无法限制获取的可以写入您提供的缓冲区的字符数,因此无法防止缓冲区溢出。如果确实需要使用C I/O库,则应改用fgets,它允许您指定要读取的最大字符数 您看到此错误的原因是您使用的键类型必须具有某种可比性。默认情况下std::map使用运算符){ m[s]++; } std::string已经提供了操作符第一件事:永远不要使用get。它无法安全使用,任何使用它的程序都存在安全漏洞

回答后编辑:


您需要实现
您需要首先实现
第一件事:永远不要使用
get
。它无法安全使用,任何使用它的程序都存在安全漏洞。无法限制
获取的
可以写入您提供的缓冲区的字符数,因此无法防止缓冲区溢出。如果确实需要使用C I/O库,则应改用
fgets
,它允许您指定要读取的最大字符数

您看到此错误的原因是您使用的键类型必须具有某种可比性。默认情况下
std::map
使用
运算符){
m[s]++;
}

std::string
已经提供了
操作符第一件事:永远不要使用
get
。它无法安全使用,任何使用它的程序都存在安全漏洞。无法限制
获取的
可以写入您提供的缓冲区的字符数,因此无法防止缓冲区溢出。如果确实需要使用C I/O库,则应改用
fgets
,它允许您指定要读取的最大字符数

您看到此错误的原因是您使用的键类型必须具有某种可比性。默认情况下
std::map
使用
运算符){
m[s]++;
}

std::string
已经提供了
操作符为了插入映射,映射需要能够比较id。您还没有提供它可以使用的运算符<的实现。您有两个选择:

  • 这里提供一个由另一个答案给出的示例
  • 改用std::string

  • 我认为您应该使用std::string。需要时,可以使用.c_str()方法将其转换为字符数组。

    为了插入到映射中,映射需要能够比较id。您还没有提供它可以使用的运算符<的实现。您有两个选择:

  • 这里提供一个由另一个答案给出的示例
  • 改用std::string

  • 我认为您应该使用std::string。需要时,可以使用.c_str()方法将其转换为字符数组。

    No
    fgets
    是C标准库的一部分,因此需要一个
    文件*
    ,就像
    stdin
    。我知道你的方法是最佳实践,这是我第一次尝试,在我问这个问题之前很久。但它一直在“超过时限”。非常感谢你。(现在我的答案被接受了,
    get
    …)不
    fgets
    是C标准库的一部分,因此需要一个
    文件*
    ,就像
    stdin
    。我知道你的方法是最佳实践,这是我第一次尝试,在我问这个问题之前很久。但它一直在“超过时限”。非常感谢你。(现在我的答案被接受了,
    get
    …)你的答案正是我想要的。非常感谢你。(有关代码的问题在问题编辑中解释。我希望这是堆栈溢出工作的最佳方式。顺便说一下,我的答案终于被接受了。)你的答案正是我想要的。非常感谢你。(有关代码的问题已在问题编辑中解释。我希望这是处理堆栈溢出的最佳方式。顺便说一句,我的答案终于被接受了。)
    #include <stdio.h>
    #include <map>
    
    using namespace std;
    
    class id {
    public:
        char v [30];
    };
    
    int main () {
        map<id, int> m;
        id a;
        while (gets(a.v)) {
            m[a]++;
        }
        return 0;
    }
    
    /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = id]’:
    /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_map.h:418:   instantiated from ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = id, _Tp = int, _Compare = std::less<id>, _Alloc = std::allocator<std::pair<const id, int> >]’
    prog.cpp:15:   instantiated from here
    /usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’
    
    class id {
    public:
        char v [30];
        bool operator<(const id &rhs) const{
            return strcmp(v,rhs.v) < 0;
        }
    };
    
    std::map<std::string, int> m;
    std::string s;
    while (std::cin >> s) {
        m[s]++;
    }