将代码从 c++ 11 转换为 c++ 98

Converting a code from c++ 11 to c++ 98?

本文关键字:c++ 转换 代码      更新时间:2023-10-16

我是 c++ 的初学者,我的编译器(无 c-free 5.0)无法编译此代码:-

#include <iostream>
#include <map>
#include <string>
int main()
{
std::string input = "slowly";
std::map<char, int> occurrences;
for (char character : input)
{
    occurrences[character] += 1;
}
for (auto& entry : occurrences)
{
    std::cout << entry.first << '=' << entry.second << std::endl;
}
}

任何人都可以告诉我如何在我的编译器中工作吗?

  • 使用迭代器将基于范围的 for 转换为循环
  • 停止使用 auto 并手动编写类型

法典:

#include <iostream>
#include <map>
#include <string>
int main()
{
    std::string input = "slowly";
    std::map<char, int> occurrences;
    for (std::string::iterator character = input.begin(); character != input.end(); character++)
    {
        occurrences[*character] += 1;
    }
    for (std::map<char, int>::iterator entry = occurrences.begin(); entry != occurrences.end(); entry++)
    {
        std::cout << entry->first << '=' << entry->second << std::endl;
    }
}