C++ 处理向量集时出错
我是标准模板库的新手,我想存储重叠的子序列。因为集合忽略重复项。我有一组向量:C++ 处理向量集时出错,c++,vector,stl,set,C++,Vector,Stl,Set,我是标准模板库的新手,我想存储重叠的子序列。因为集合忽略重复项。我有一组向量: set<vector<int> > subsequences; 我得到一个错误: coinChange.cpp:20:18: error: no matching member function for call to 'insert' subsequences.insert(arr); ~~~~~~~~~~~~~^~~~~~ /Applications/Xcode.app/
set<vector<int> > subsequences;
我得到一个错误:
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)
^
coinChange.cpp:20:18:错误:调用“insert”时没有匹配的成员函数
子序列。插入(arr);
~~~~~~~~~~~~~^~~~~~
/Applications/Xcode.app/Contents/Developer/toolschains/XcodeDefault.xctoolschain/usr/bin/。/include/c++/v1/set:596:25:注意:
候选函数不可行:第一个参数没有从“vector”到“const vector”的已知转换
对插入(常数值类型和v)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/。/include/c++/v1/set:613:14:注意:
候选函数模板不可行:需要2个参数,但提供了1个
无效插入(\u输入计算器\u\f,\u输入计算器\u\l)
^
/Applications/Xcode.app/Contents/Developer/toolschains/XcodeDefault.xctoolschain/usr/bin/。/include/c++/v1/set:604:14:注意:
候选函数不可行:需要2个参数,但提供了1个
迭代器插入(常量迭代器、常量值、类型和类型)
^
由于我是STL新手,我无法理解我做错了什么。请提供帮助。错误消息显示出问题:
candidate function not viable: no known conversion from 'vector<long long, allocator<long long>>' to 'const vector<int, allocator<int>>' for 1st argument
候选函数不可行:第一个参数没有从“vector”到“const vector”的已知转换
您正在尝试将向量
传递到set::insert
中vector
与vector
是不同的类型,这就是它不起作用的原因。要解决此问题,请始终使用相同类型的向量。错误显示“从向量
到常量向量
看起来您正试图在应该包含int向量的集合中插入long long向量。鉴于此错误,您很可能声明了一个
std::vector
,并假设它与std::vector
兼容。事实并非如此
如果T
和U
是不同的类型,那么std::vector
和std::vector
也是不同的类型。在您的例子中,T
是int
,而U
是长的
以下代码编译正确:
#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);
}
什么是arr
声明的?这里没有问题:
#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 std::vector<int> IntVector; // change this to long long if needed
//...
std::set<IntVector> subsequences;
IntVector arr;
//...