我正在制作一个接受向量的函数,对其进行排序,然后使用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] 删除。
我来说两句