处理向量集时出错
error in handling set of vectors
我是标准模板库的新手,我想存储重叠的子序列。由于 set 忽略重复项。我有一组向量:
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 的新手,所以我无法理解我做错了什么。请帮忙。
错误消息说明出了什么问题:
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>>
的转换
看起来您正试图在本应包含整数向量的集合中插入一个长向量。
鉴于此错误,您很可能声明了 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;
//...
我是标准模板库的新手,我想存储重叠的子序列。由于 set 忽略重复项。我有一组向量:
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 的新手,所以我无法理解我做错了什么。请帮忙。
错误消息说明出了什么问题:
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>>
看起来您正试图在本应包含整数向量的集合中插入一个长向量。
鉴于此错误,您很可能声明了 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;
//...