标签: stdset

使用C++中的集合

如何在C++中定义集合并将值插入其中?

c++ set stdset

3
推荐指数
1
解决办法
753
查看次数

set :: insert是保存副本还是指针C++

函数是否set::insert保存指向元素或其副本的指针.意思是,我可以执行以下代码,还是必须确保指针不被删除?

int *a;
*a=new int(1);
set<int> _set;
_set.insert (*a);
delete a;
*a=new int(2);
_set.insert (*a);
delete a;
Run Code Online (Sandbox Code Playgroud)

我给出了示例int,但我的真实程序使用了我创建的类.

c++ set stdset

3
推荐指数
1
解决办法
4493
查看次数

同样的'价值'对于std :: set意味着什么?

在C++中,std :: set :: insert()仅在没有具有相同"value"的值时才插入值.同样,这是否意味着运算符==或者它是否意味着哪个运算符<对于任何一个排序都是假的,或者它是否意味着其他东西?

c++ comparison stl insert stdset

3
推荐指数
1
解决办法
297
查看次数

如何在std :: map中找到min/max,就像在std :: set中一样?

由于set和map都是有序容器,因此std :: map中的min和max可以在0(1)时间内找到,就像std :: set一样?

// for std::set
// std::set<int> s;
auto min = *s.begin();

auto max = *s.rbegin();
Run Code Online (Sandbox Code Playgroud)

如何从std :: map中获取O(1)中的max和min?这里的其他问题似乎建议迭代遍历地图,但我们不能使用std :: map的有序rightlt来更快地获得结果吗?

c++ stdmap set stdset c++11

3
推荐指数
1
解决办法
1805
查看次数

std ::为2D点设置自定义比较器

我需要一个非重复的2D点列表,所以我使用了std::set一个自定义比较函数.我使用的函数在插入点后有问题,因为有时候std::find找不到已经插入的点.

const double tolerance = 0.1;
struct MyPoint2D
{
  MyPoint2D(double x, double y) : _x(x), _y(y) {}
  double _x, _y;
};
auto compMyPoint2D = [&](const MyPoint2D& pointA, const MyPoint2D& pointB) -> bool
{
  if (pointA._x < pointB._x - tolerance) return true;
  if (pointA._x > pointB._x + tolerance) return false;
  if (pointA._y < pointB._y - tolerance) return true;
  return false;
};
std::set<MyPoint2D, decltype(compMyPoint2D)> orderedMyPoints(compMyPoint2D);
MyPoint2D pointA(0.66,1.14);
MyPoint2D pointB(0.75, 0.0);
MyPoint2D pointC(0.57,1.19);
orderedMyPoints.insert(pointA);
orderedMyPoints.insert(pointB);
orderedMyPoints.insert(pointC);
if (orderedMyPoints.find(pointC)==orderedMyPoints.end())
{ …
Run Code Online (Sandbox Code Playgroud)

c++ std stdset c++11

2
推荐指数
2
解决办法
753
查看次数

复制std :: set成员变量而不是在移动类实例时移动

我一直在和一对班级一起工作.前者存储元数据,后者作为容器,支持基于元数据的各种索引.剥离版本发布在下面.

后者类使用的std ::设置来管理其前级的对象的集合,以指涉稳定性方面的原因(如元素被添加和删除指针组成的元数据对象必须保持有效).

由于我不理解的原因,即使应该调用移动语义,索引类的set成员也会调用其(已删除的)复制构造函数.我已经在Apple LLVM 7.0.0(使用libc ++)和GCC 4.9(libstdc ++)上编译并收到了类似的错误.

是否有某些原因在这种情况下无法调用移动构造函数?

#include <ctime>
#include <functional>
#include <set>
#include <string>
#include <memory>
#include <vector>

// used to store meta data about some class T
template<typename T>
struct Foo {
  std::unique_ptr<T> data; // T being some abstract class
  std::string label;
  std::time_t stamp;
  const Foo* parentPtr;

  Foo( std::unique_ptr<T>&& data,
        const std::string& label,
        const std::time_t stamp,
        const Foo* parent ) : data( std::move(data) ),
                         label( label ),
                         stamp( stamp ),
                         parentPtr( parent ){}
};

// …
Run Code Online (Sandbox Code Playgroud)

c++ gcc stdset c++14

2
推荐指数
1
解决办法
218
查看次数

比std :: set更快的查找

我需要对一些遗留数据包处理代码进行更快的成员资格查找,这需要识别具有特定ID的数据包是否在特定列表中.

该列表仅每隔几秒更新一次,而数据包匹配经常发生,因此查找性能比插入/删除等更重要.

一般流程:

forall(special_PacketIDs)
{
  pktIdSet.insert(theSpecialPktId)
}

while (1)
{
  pkt = readPkt();
  pktID = getPktIdOfPkt(pkt);

  if ( aSpecialPkt(pktID) )
    doSomething();
}
Run Code Online (Sandbox Code Playgroud)

现在,aSpecialPkt(pktId)定义为:

bool PktProcessor::aSpecialPkt(unsigned short pid)
{
  return pktPidSet.find(pid) != pktPidSet.end();
}
Run Code Online (Sandbox Code Playgroud)

gprof报告了在std :: set :: find()中花费的大量时间

pktId的范围仅为8192个可能的值.以内存为代价分配线性阵列会更快,例如:

class LinearSet
{
public:
  void insert(pid) { mPktIdSet[pid] = true; }
  bool elementExists(pid)  { return mPktIdSet[pid]; }
private:
  bool mPktIdSet[8192];
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,在保持最佳性能的同时,是否有更多的"C++"方法可以做到这一点?

c++ stdset

2
推荐指数
1
解决办法
200
查看次数

如何更新一组std :: pair中的成员?

我试过这样做:

std::set< pair<int, int> > mySet;

// fill the set with something

mySet.find( make_pair(someValueX, someValueY) )->first = newX;
Run Code Online (Sandbox Code Playgroud)

但是我在编译时遇到以下错误:

error: assignment of member 'std::pair<int, int>::first' in read-only object|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===||
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl set stdset

2
推荐指数
1
解决办法
1070
查看次数

如何根据 set.begin() 的偏移量迭代 std::set?

我需要获得一个基于偏移量的迭代器。

即有开始的迭代器:

auto it = set.begin()
Run Code Online (Sandbox Code Playgroud)

我需要到达具有偏移量的迭代器ofst

it + ofst
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?我需要增量增加it++迭代器ofst次数。

c++ algorithm iterator stdset c++11

2
推荐指数
1
解决办法
86
查看次数

如何在 std::variant 的向量中过滤掉某些数据类型的元素?

我有一个类型为或std::vectorstd::variant元素。如果迭代元素的类型为 ,我想遍历这个向量和一个额外的项目。但是,似乎不允许在运行时查询索引。我怎样才能做到这一点?intstd::set<int>insertstd::set<int>

#include <variant>
#include <set>
#include <vector>

int main()
{
    using Variants = std::variant<int, std::set<int>>;

    std::vector<Variants> var_vec;
    var_vec.push_back(999);
    std::set<int> a = {0,1,2};
    var_vec.push_back(a);

    for (int i = 0; i < var_vec.size(); ++i)
    {
        // if the element var_vec[i] is of type std::set<int>
        if (var_vec[i].index() == 1) var_vec[i].insert(888);   // !ERROR! How to achieve this?
    }
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误信息:

error: '__gnu_cxx::__alloc_traits<std::allocator<std::variant<int, std::set<int, std::less<int>, std::allocator<int> > > >, std::variant<int, std::set<int, std::less<int>, std::allocator<int> > …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector stdset c++17 std-variant

2
推荐指数
1
解决办法
82
查看次数

标签 统计

c++ ×10

stdset ×10

set ×4

c++11 ×3

iterator ×2

stl ×2

algorithm ×1

c++14 ×1

c++17 ×1

comparison ×1

gcc ×1

insert ×1

std ×1

std-variant ×1

stdmap ×1

stdvector ×1