我需要复制std::set
到std::vector
:
std::set <double> input;
input.insert(5);
input.insert(6);
std::vector <double> output;
std::copy(input.begin(), input.end(), output.begin()); //Error: Vector iterator not dereferencable
Run Code Online (Sandbox Code Playgroud)
问题出在哪儿?
我正在大量使用,std::set<int>
而且我经常需要检查这样的集合是否包含数字.
我觉得写作很自然:
if (myset.contains(number))
...
Run Code Online (Sandbox Code Playgroud)
但由于缺少一名contains
成员,我需要编写繁琐的内容:
if (myset.find(number) != myset.end())
..
Run Code Online (Sandbox Code Playgroud)
或者不那么明显:
if (myset.count(element) > 0)
..
Run Code Online (Sandbox Code Playgroud)
这个设计决定有理由吗?
我一直在尝试在C++中找到两个std :: set之间的交集,但我一直收到错误.
我为此创建了一个小样本测试
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int main() {
set<int> s1;
set<int> s2;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
s2.insert(1);
s2.insert(6);
s2.insert(3);
s2.insert(0);
set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
后一个程序不生成任何输出,但我希望有一个新的集合(让我们称之为s3
)具有以下值:
s3 = [ 1 , 3 ]
Run Code Online (Sandbox Code Playgroud)
相反,我得到错误:
test.cpp: In function ‘int main()’:
test.cpp:19: error: no matching function for call to ‘set_intersection(std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>)’
Run Code Online (Sandbox Code Playgroud)
我从这个错误中理解的是,没有定义set_intersection
接受Rb_tree_const_iterator<int>
参数.
此外,我想该std::set.begin()
方法返回这种类型的对象,
有没有更好的方法std::set
在C++中找到两个的交集?最好是内置功能?
非常感谢!
我有一个std::set<int>
,找到这个集合中最大的int的正确方法是什么?
这可能是一个愚蠢的问题,我对C++和编程很新.我想了解几个STL容器的使用,考虑到这一点,我想知道使用std :: set vs使用矢量或地图的优点是什么?我似乎无法找到这个问题的明确答案.我注意到集合使用地图,但为什么不总是使用地图或总是使用集合.而是提供了两个非常相似的容器.提前致谢.
我有一个关于std :: set的线程安全性的问题.
据我所知,我可以遍历一个集合并添加/擦除成员,这不会使迭代器无效.
但请考虑以下情况:
程序运行时我经历过段错误,我不确定为什么会这样.缺乏线程安全的原因是什么?
我想知道为什么std::map
并std::set
使用std::less
默认仿函数来比较键.为什么不使用类似于strcmp的仿函数?就像是:
template <typename T> struct compare
{
// Return less than 0 if lhs < rhs
// Return 0 if lhs == rhs
// Return greater than 0 if lhs > rhs
int operator()(T const& lhs, T const& rhs)
{
return (lhs-rhs);
}
}
Run Code Online (Sandbox Code Playgroud)
说一个map
有两个对象,用键key1
和key2
.现在我们要插入另一个带键的对象key3
.
使用时std::less
,该insert
功能需要先std::less::operator()
用key1
和调用key3
.假设std::less::operator()(key1, key3)
返回false.它必须std::less::operator()
再次通过键切换std::less::operator()(key3, key1)
,以决定是否key1
等于 …
考虑下面的简单程序,它尝试使用对其中元素的NON-const引用来遍历集合的值:
#include <set>
#include <iostream>
class Int
{
public:
Int(int value) : value_(value) {}
int value() const { return value_; }
bool operator<(const Int& other) const { return value_ < other.value(); }
private:
int value_;
};
int
main(int argc, char** argv) {
std::set<Int> ints;
ints.insert(10);
for (Int& i : ints) {
std::cout << i.value() << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时,我从gcc收到错误:
test.c: In function ‘int main(int, char**)’:
test.c:18:18: error: invalid initialization of reference of type ‘Int&’ from expression of type …
Run Code Online (Sandbox Code Playgroud) 如果it1和it2之间有什么区别?
std::set<sometype> s;
auto it1 = std::inserter(s, s.begin());
auto it2 = std::inserter(s, s.end());
Run Code Online (Sandbox Code Playgroud)