小编Иве*_*нов的帖子

无法理解头文件中的静态(内联)

我已经使用静态很长时间了,但是现在当头文件出现时我有点困惑。

主要问题是这段代码:

#include <iostream>
#include <string>
#include <sstream>
#include <set>
#ifndef WORD_H
#define WORD_H

#include<unordered_map>

class Word
{
private:
    std::string word;
    int k;

    static std::unordered_map<std::string, int> x;
public:

    Word(std::string word) : word(word)
    {
        if (x.find(word) != x.end())
            x.insert({ word , 0 });
        else
        {
            x[word]++;
        }
    }

    std::string getWord() const
    {
        return x.find(this->word)->first;
    }

    int getCount() const
    {
        return x.find(this->word)->second;
    }
    friend bool operator<(const Word& a, const Word& b);
};

bool operator<(const Word& a, const Word& b)
{
    return a.getWord() …
Run Code Online (Sandbox Code Playgroud)

c++ static

-1
推荐指数
1
解决办法
60
查看次数

标签 统计

c++ ×1

static ×1