C ++排序错误“没有重载函数的实例." [英] C++ Sort Error "No instance of overloaded function.."

查看:84
本文介绍了C ++排序错误“没有重载函数的实例."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般对C ++和编程还是很陌生,所以如果第一次没有正确的信息,我深表歉意

我从Bjarne Stroustrup撰写的编程:使用C ++的原理和实践(第二版)"一书中开始学习如何编码,并且在使用第4.6.4章中提供的代码时遇到了一些错误.每次我运行代码时,它都会告诉我有关"std :: sort"的信息,并且没有重载函数"std :: sort"的实例与参数列表匹配.由于IDE(Visual Studio 2013 Express)表示标识符未定义,因此i-1的第16行也出现了新错误.

I started learning how to code with the book "Programming: Principles and Practice Using C++ (2nd Edition)" by Bjarne Stroustrup and I ran into some errors while using the code provided in chapter 4.6.4. Every time I go to run the code it tells me about "std::sort" and that there's no instance of overloaded function "std::sort" matches the argument list. There's also a new error in line 16 with i-1 as the IDE (Visual Studio 2013 Express) says the identifier is undefined.

#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时,我似乎无法找出导致错误的原因,但它仍然显示错误.任何解释都会有很大帮助.

I can't seem to find out what's causing the error as I've put the required #include but it still shows the error. Any explanation would help tremendously.

推荐答案

std :: sort需要一对迭代器.

std::sort takes a pair of iterators.

std::sort(words.begin(), words.end());

您可以定义自己的带有一个参数的辅助函数.

You could define your own helper function that takes one argument.

template<typename Container>
inline void sort(Container& c)
{
    std::sort(std::begin(c), std::end(c));
}

您可能想为助手功能创建自己的命名空间.

You probably want to create your own namespace for the helper function.

这篇关于C ++排序错误“没有重载函数的实例."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆