处理向量集时出错
error in handling set of vectors
我是标准模板库的新手,我想存储重叠的子序列。因为集合忽略重复项。我有一组向量:
set<vector<int> > subsequences;
但是当我尝试插入此集合时:
sort(arr.begin(), arr.end());
subsequences.insert(arr);
我收到一个错误:
coinChange.cpp:20:18: error: no matching member function for call to 'insert'
subsequences.insert(arr);
~~~~~~~~~~~~~^~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/set:596:25: note:
candidate function not viable: no known conversion from 'vector<long long, allocator<long
long>>' to 'const vector<int, allocator<int>>' for 1st argument
pair<iterator,bool> insert(const value_type& __v)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/set:613:14: note:
candidate function template not viable: requires 2 arguments, but 1 was provided
void insert(_InputIterator __f, _InputIterator __l)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/set:604:14: note:
candidate function not viable: requires 2 arguments, but 1 was provided
iterator insert(const_iterator __p, const value_type& __v)
^
由于我是STL的新手,我无法理解我做错了什么。请帮忙。
鉴于错误,您很可能声明了一个std::vector<long, long>
,并假设它与std::vector<int>
兼容。 事实并非如此。
如果T
和U
是不同的类型,那么std::vector<T>
和std::vector<U>
也是不同的类型。 在您的情况下,T
是int
,而U
是long long
。
以下代码正确编译:
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
std::set<vector<int> > subsequences;
std::vector<int> arr;
std::sort(arr.begin(), arr.end());
subsequences.insert(arr);
}
而以下代码会产生您看到的错误:
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
std::set<vector<int> > subsequences;
std::vector<long long> arr;
std::sort(arr.begin(), arr.end());
subsequences.insert(arr);
}
因此,显而易见的解决方法是确保您使用相同的类型。 确保这一点的一种方法是使用 typedef
,并确保在代码库中使用它来表示要使用的向量类型:
//...
typedef std::vector<int> IntVector; // change this to long long if needed
//...
std::set<IntVector> subsequences;
IntVector arr;
//...
错误消息说明了问题所在:
candidate function not viable: no known conversion from 'vector<long long, allocator<long long>>' to 'const vector<int, allocator<int>>' for 1st argument
您正在尝试将vector<long long>
传递到set<vector<int>>::insert
。 vector<long long>
是与vector<int>
不同的类型,这就是为什么它不起作用的原因。要修复它,请始终使用相同类型的矢量。
错误显示"没有从vector<**long long**, allocator<long
long>>
到const vector<**int**, allocator<int>>
的已知转换
看起来您正在尝试在应该包含整数向量的集合中插入一个长长的向量。
相关文章:
- 在 c++ 的构造函数中分配对象向量时出错
- 使用复向量时出错
- 我在尝试将多个值push_back向量时出错。我做错了什么?
- parallel_for lambda 函数填充 2D 向量后出错
- 修改另一个类的向量成员时出错
- 使用类作为向量类型时出错 - "Undeclared identifier"
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- Cgal::D ynamic_dimension_tag 在尝试构建点向量时出错
- 当试图将C++矩阵和向量乘法Seg存储到求和中时,它们会出错
- 在成对向量中查找函数时出错
- 创建局部结构的 std::向量时出错
- 尝试在头文件上私下声明向量时出错
- 搜索向量时出错
- 将一个向量(从向量的一个向量中提取)分配给另一个向量时出错,运算符为
- 将int推送到向量时出错
- Visual Studio报告对结构向量的push_back()函数调用出错
- 对向量进行操作后返回时出错
- 在向量中遍历结构成员时出错
- 将随机数与向量大小进行比较时出错
- g++ 编译器调用函数时出错(向量<向量<int> > &)