在C ++中使用auto时无法理解错误

b骨者

我正在制作一个接受向量的函数,对其进行排序,然后使用unique删除重复项,但是在编写unique时出现错误,即“ end_unique”变量没有数据类型。我不知道该怎么办。请帮忙!!!

   #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void elidup(vector<string> &w)
{
     // sort function is used to place words alphabetically so that after that we can find duplicates
     sort(w.begin(), w.end());
     // unique helps rearrange the input to "eliminate" adjacent duplicate entries
     auto end_unique = unique(w.begin(), w.end());
     // erase is used to eliminate unused elements
     w.erase(end_unique, w.end());
}
int main()
{
    string inp;
    cout<<" Enter the vector in which duplicates are to be found. Input * to indicate that it is the end of the string"<<endl;
    vector<string> word;
    while(inp != "*")
    // loop to enter the information in the vector
        {
            cin>>inp;
            word.push_back(inp);
        }
    if(inp == "*")
    // for erasing the duplicates and then print the vector
    {
        elidup(word);
        for(int i = 0 ; i<(word.size() -1);i++)
            cout<<word[i]<<" " ;
    }
    return 0;
}

这是编译屏幕显示的内容

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\admin\Desktop\Dhairya\522031-10-9E.cpp||In function `void elidup(std::vector<std::string, std::allocator<std::string> >&)':|
C:\Users\admin\Desktop\Dhairya\522031-10-9E.cpp|11|error: ISO C++ forbids declaration of `end_unique' with no type|
C:\Users\admin\Desktop\Dhairya\522031-10-9E.cpp|11|error: cannot convert `__gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >' to `int' in initialization|
C:\Users\admin\Desktop\Dhairya\522031-10-9E.cpp|13|error: no matching function for call to `std::vector<std::string, std::allocator<std::string> >::erase(int&, __gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >)'|
C:\Dev-Cpp\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\vector.tcc|108|note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc = std::allocator<std::string>]|
C:\Dev-Cpp\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\include\c++\3.4.2\bits\vector.tcc|120|note:                 typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc = std::allocator<std::string>]|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
先生们

我刚刚检查了您的代码-此处简体版,即C ++编译器的网络版本。它仅使用C ++ 14即可工作!那么,您的实际问题是什么?您也可以替换这两行

// unique helps rearrange the input to "eliminate" adjacent duplicate entries
auto end_unique = unique(w.begin(), w.end());
// erase is used to eliminate unused elements
w.erase(end_unique, w.end());

通过这一行:

w.erase(unique(w.begin(), w.end()), w.end());

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void elidup(vector<string> &words)
{
     // sort function is used to place words alphabetically so that after that we can find duplicates
     sort(words.begin(), words.end());
     // unique helps rearrange the input to "eliminate" adjacent duplicate entries
     auto end_unique = unique(words.begin(), words.end());
     // erase is used to eliminate unused elements
     words.erase(end_unique, words.end());
}
int main()
{
    vector<string> words{"ab", "cd", "ab", "cd"};
    elidup(words);
    for(auto w: words) cout<<w<<endl;
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在使用指向指针的指针时无法理解我的错误 (C)

来自分类Dev

C ++:无法理解编译错误

来自分类Dev

无法理解混淆的C代码

来自分类Dev

C ++模板无法理解的行为

来自分类Dev

C语言。无法理解指针的输出

来自分类Dev

无法理解混淆的C代码

来自分类Dev

无法理解C程序输出

来自分类Dev

无法理解C中的文件范围

来自分类Dev

无法理解这行代码的C ++

来自分类Dev

无法理解C中的Null指针

来自分类Dev

无法理解这段C ++代码

来自分类Dev

无法理解函数输出 (C)

来自分类Dev

无法理解类 C++

来自分类Dev

非常简单的代码,并出现错误C2712,无法理解原因

来自分类Dev

我无法理解C#的统一和错误的简单答题器

来自分类Dev

如何在C ++中使用WMI从WIN32_NetworkAdapter检索值?我无法理解查询后用于访问值的VARIANT

来自分类Dev

我如何在vb.net中使用此查询-我无法理解从C#转换为vb.net的情况

来自分类Dev

C ++语法理解问题,“使用”

来自分类Dev

c ++无法理解使用智能指针的基本概念

来自分类Dev

无法理解使用strchr来获取C中字符串中字符的位置

来自分类Dev

无法理解错误:

来自分类Dev

无法理解错误

来自分类Dev

无法理解错误

来自分类Dev

无法理解C中联合程序的输出

来自分类Dev

C中位域的内存布局-无法理解输出

来自分类Dev

我无法理解c ++中char指针操作的索引

来自分类Dev

C#无法理解正则表达式

来自分类Dev

无法理解C程序中“系统”功能调用的行为

来自分类Dev

无法理解在C ++中此函数的返回