我可以做一个简单的数组:
set < char > * words = new set < char > [10]
我怎么做一组集合?这会导致编译器错误:
vector < set< char >> v.谢谢你的回答!
我有一个std::string。我想要一组唯一的字符,每个字符都用表示std::string。
我可以轻松获得一组字符:
std::string some_string = ...
std::set<char> char_set(some_string.begin(), some_string.end());
Run Code Online (Sandbox Code Playgroud)
我可以将它们转换为这样的字符串:
std::set<std::string> string_set;
for (char c: char_set) {
string_set.emplace(1, c);
}
Run Code Online (Sandbox Code Playgroud)
但是这种方法似乎很尴尬。是否有更好的方法(最好是标准库单行)?
之间有什么区别集key_comp :: VS集:: value_comp在C ++?转到 cplusplus.com 页面没有显着差异。此外,在 set::key_comp 和相关 set::value_comp 页面上的最后一句是“(...) key_comp 及其兄弟成员函数 value_comp 是等效的。”
示例几乎相同:
我想使用lambda表达式作为std ::整数集的自定义比较.该网站上有许多解释如何执行此操作的答案,例如/sf/answers/3228982501/.事实上,
#include <vector>
#include <set>
#include <iostream>
int main() {
auto different_cmp = [](int i, int j) -> bool {
return j < i;
};
std::set<int, decltype(different_cmp)> integers(different_cmp);
integers.insert(3);
integers.insert(4);
integers.insert(1);
for (int integer : integers) {
std::cout << integer << " ";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编制和输出
4 3 1
正如所料.但是,当我尝试将此设置放在矢量中时
std::vector<std::set<int, decltype(different_cmp)>> vec_of_integers;
vec_of_integers.push_back(integers);
Run Code Online (Sandbox Code Playgroud)
编译器抱怨.我正在使用Visual Studio 2017,我根据周围的代码得到不同的编译器错误.在上面的例子中,它是
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\utility(77): error C2664: 'void std::swap(std::exception_ptr &,std::exception_ptr &) noexcept': cannot convert argument 1 from '_Ty' …Run Code Online (Sandbox Code Playgroud) 假设我有一组(或映射)字符串,并且我想使用仅比较前 5 个字符的自定义比较器。所以“abcde”和“abcdef”在我的集合中是相同的。
using MySet = std::set<std::string, Cmp>;
Run Code Online (Sandbox Code Playgroud)
编写 Cmp 的最佳方式是什么?
明显的方法是这样的:
struct Cmp
{
bool operator()(const string& x, const string& y) const
{
return x.substr(0, 5) < y.substr(0, 5);
}
};
Run Code Online (Sandbox Code Playgroud)
问题是这段代码重复了.substr(0, 5)。在这个例子中它很短,但在一般情况下它可能会更长。我想避免这个重复的代码。
一般来说,给定类型T1, T2和函数T2 key(T1& const),我想要一组T1根据 进行比较的元素key(a) < key(b),其中比较 onT2已经明确定义。写这个的最好方法是什么?我考虑过编写一个新的class KeyBaseSet,但这对于我的单一用例来说是过度设计的。有什么方法可以使用stdBoost 来做到这一点吗?
我正在寻找类似于keyPython 中排序时的参数(https://docs.python.org/3/howto/sorting.html#key-functions),或compare `on` Haskell 中的习惯用法(https://stackoverflow.html)。 com/a/2788262/351105)。
所以我在std :: set上遇到了一个奇怪的迭代器bug:我无法在没有编译错误的迭代器上做一个像(it + 1)这样简单的事情尝试自己编译:
void setBug()
{
std::set<int> values;
for (auto it = values.cbegin();
it != values.cend(); ++it) {
if ((it + 1) != values.end())
values.insert(*it / *(it + 1));
}
}
Run Code Online (Sandbox Code Playgroud)
错误:二进制表达式的无效操作数('std :: _ 1 :: _tree_const_iterator*,long>'和'int')if((it + 1)!= values.end())
错误:二进制表达式的无效操作数('std :: _ 1 :: _tree_const_iterator*,long>'和'int')values.insert(*it/*(it + 1));
编译器版本:Apple LLVM版本5.0(clang-500.2.79)(基于LLVM 3.3svn)目标:x86_64-apple-darwin13.1.0线程模型:posix
我找到了一个肮脏的解决方案:( auto it2 = ++ it; --it)有效,但这真的很脏......
有人解释一下吗?std :: set坏了吗?
谢谢.
我有一个std::map<int, std::set<int>>名为misi. 我想知道为什么misi.emplace(2345, {6, 9});并且misi.emplace({2345, {6, 9}});没有按预期工作,如下所示。
#include <set> // std:set
#include <map> // std::map
#include <utility> // std::piecewise_construct, std::pair
#include <tuple> // std::forward_as_tuple
#include <iostream> // std::cout, std::endl
int main()
{
// --- std::set initializer list constructor ---
std::set<int> si({42, 16});
std::cout << "si.size(): " << si.size() << std::endl; // 2
std::cout << "*si.begin(): " << *si.begin() << std::endl; // 16
// --- std::set emplace() ---
si.emplace(7);
std::cout << …Run Code Online (Sandbox Code Playgroud) 我想set从 a 的每个条目的成员变量的内容中创建和填充a vector。这就是我正在做的:
struct S { int i; };
int main()
{
std::vector<S*> structPtrs;
// code to fill the above vector
// create set from the above vector
std::set<int> setInts;
for (auto it = structPtrs.begin(); it != structPtrs.end(); ++it)
{
setInts.insert((*it)->i);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有 STL 方法来做到这一点?或者通过任何可用的方法<algorithm>?
我知道std :: set不允许非const访问它的项目.我知道将项目移出集合是不可能的 - 因为任何类型的非const访问都可能破坏集合的顺序.
但是,我们可以从集合中删除项目.这不会破坏它,因为它只会强制重组.那怎么回事,我们不能'弹出'一个项目?为什么我不能拿出物品并同时擦掉它?
我问的原因是 - 我需要一个有序的unique_ptrs容器.偶尔我需要从一个容器中"弹出"unique_ptrs并将它们转移到另一个容器中.必须订购它们才是我制作的定制仿函数.
我不明白为什么不应该允许弹出功能?
我有一个类,它支持克隆(通过方法clone)。我在 的向量中有一堆它的实例std::unique_ptr。
现在,我想std::set从上面的向量创建一个相同的智能指针,最好是在其构造过程中。明显的设计如下:
#include <memory>
#include <set>
#include <vector>
class A
{
public:
/// type of itself
typedef A self;
A() = default;
A(const self& another) = default;
virtual ~A() = default;
std::unique_ptr<A> clone() const
{
return std::make_unique<A>();
}
};
class SetOfA
{
public:
SetOfA() = default;
// Here is the method I would like to improve
SetOfA(const std::vector<std::unique_ptr<A> >& data)
{
//do not like this loop, prefer this to be in initialization part? …Run Code Online (Sandbox Code Playgroud)