C++ 排序错误 "No instance of overloaded function.."
C++ Sort Error "No instance of overloaded function.."
我对 C++ 和一般编程还是很陌生,所以如果第一次没有正确的信息,我深表歉意
我开始学习如何使用 Bjarne Stroustrup 的书 "Programming: Principles and Practice Using C++ (2nd Edition)" 编写代码,并且在使用第 4.6.4 章中提供的代码时 运行 遇到了一些错误。每次我转到 运行 时,它都会告诉我有关 "std::sort" 的代码,并且没有重载函数的实例 "std::sort" 与参数列表匹配。由于 IDE (Visual Studio 2013 Express) 表示标识符未定义,因此第 16 行中的 i-1 也出现了一个新错误。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::string>words;
for (std::string temp; std::cin >> temp;)
words.push_back(temp);
std::cout << "Number of words: " << words.size() << std::endl;
std::sort(words);
for (int i = 0; i<words.size(); ++i)
if (i == 0 || words[i–1] != words[i]) // is this a new word?
std::cout << words[i] << "\n";
}
我似乎无法找出导致错误的原因,因为我已经输入了所需的#include,但它仍然显示错误。任何解释都会有很大帮助。
std::sort 采用一对迭代器。
std::sort(words.begin(), words.end());
您可以定义自己的带一个参数的辅助函数。
template<typename Container>
inline void sort(Container& c)
{
std::sort(std::begin(c), std::end(c));
}
您可能想为辅助函数创建自己的命名空间。
我对 C++ 和一般编程还是很陌生,所以如果第一次没有正确的信息,我深表歉意
我开始学习如何使用 Bjarne Stroustrup 的书 "Programming: Principles and Practice Using C++ (2nd Edition)" 编写代码,并且在使用第 4.6.4 章中提供的代码时 运行 遇到了一些错误。每次我转到 运行 时,它都会告诉我有关 "std::sort" 的代码,并且没有重载函数的实例 "std::sort" 与参数列表匹配。由于 IDE (Visual Studio 2013 Express) 表示标识符未定义,因此第 16 行中的 i-1 也出现了一个新错误。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::string>words;
for (std::string temp; std::cin >> temp;)
words.push_back(temp);
std::cout << "Number of words: " << words.size() << std::endl;
std::sort(words);
for (int i = 0; i<words.size(); ++i)
if (i == 0 || words[i–1] != words[i]) // is this a new word?
std::cout << words[i] << "\n";
}
我似乎无法找出导致错误的原因,因为我已经输入了所需的#include,但它仍然显示错误。任何解释都会有很大帮助。
std::sort 采用一对迭代器。
std::sort(words.begin(), words.end());
您可以定义自己的带一个参数的辅助函数。
template<typename Container>
inline void sort(Container& c)
{
std::sort(std::begin(c), std::end(c));
}
您可能想为辅助函数创建自己的命名空间。