标签: stdset

C++ std :: set comparator

这是代码:

struct comp
{
    bool operator()(Reputation *one, Reputation *two)
    {
        if (one->Amount < 0 && two->Amount >= 0)
            return false;
        if (one->Amount >= 0 && two->Amount < 0)
            return true;

        if (one->Amount >= 0)
            return one->Amount <= two->Amount;
        else
            return one->Amount >= two->Amount;
    }
};
Run Code Online (Sandbox Code Playgroud)

这就是问题所在:

调试断言失败!
文件:..\VC\include\xtree
行:638

表达式:无效的运算符<

之后,我可以选择"Abort","Retry"或"Ignore".如果我选择忽略更多(相同的),但它最终会完美地工作.

当我将 - > Amount ==的信誉插入到之前插入的信誉*之一时,似乎会出现问题,但我不确定最后一个.

任何帮助将不胜感激

编辑:我希望他们订购的顺序首先是按顺序排列的顺序,然后按顺序顺序排列.示例:1 5 10 11 11 20 50 -1 -5 -50

c++ set comparator stdset

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

修改std :: set中的元素

我有以下代码 - :

int main()
{
    set<string> s;
    s.insert( "asas" );
    s.insert( "abab" );

    for ( auto item : s )
    {
        cout << item << "\n";
        reverse( item.begin(), item.end() );
    }

    cout << "\n";

    for ( auto item : s )
    {
        cout << item << "\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

输出 - :

abab
asas

abab
asas
Run Code Online (Sandbox Code Playgroud)

reverse()函数根本没有修改集合的元素.
我怀疑集合中的元素根本无法修改.但是,如果是这种情况,为什么编译器本身不会出错呢?

-std=c++14在Windows 7上使用带有标志的TDM-GCC 4.9.2 .

c++ gcc stl std stdset

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

为什么标准的关联有序容器将“ const char *”作为其键?

据我所知,我们绝不应该使用关系运算符来比较两个const字符串<>。因为它比较地址而不是值:

const char* sz1 = "Hello";
const char* sz2 = "hello";
if(sz1 < sz2);// not valid. So use strcmp instead.
Run Code Online (Sandbox Code Playgroud)
  • 我注意到,有序关联容器map, multimap, set, multiset对它们施加了限制,key因此应该对键进行一些比较以对容器中的元素进行排序。键的默认运算符是<operator。

在创建之前,一切都是清楚的map, setconst char*然后得到错误的结果:

std::set<const char*> scp{ "Hello", "World", "C++", "Programming" };    
std::set<std::string> sstr{ "Hello", "World", "C++", "Programming" };

// error
std::copy(scp.cbegin(), scp.cend(), std::ostream_iterator<const char*>(std::cout, " "));
std::cout << std::endl;

// Ok 
std::copy(sstr.cbegin(), sstr.cend(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
Run Code Online (Sandbox Code Playgroud)
  • 显然,只要类已定义为可以正常工作,就可以scp将指向指针的字符串比较sstr为OK …

c++ stl stdmap stdset

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

如何比较二维空间中的两点

我有一个类可以Points在 2D 空间中保持这样的状态:

class Point{
public:
Point(double a, double b){ x = a; y = b; }

//some additional information

private:
    double x, y;
};
Run Code Online (Sandbox Code Playgroud)

我喜欢将它们放在Pointsa 中std::set,但我不知道如何编写比较结构

struct cCompare{
    bool operator()(const Point &p1, const Point &p2){
         //what should I write here??
    }
};
Run Code Online (Sandbox Code Playgroud)

p如果和 等两点q相等(p_1,p_2) = (q_1,q_2)。我是否必须在Point班级中存储一些附加信息?像索引或每个的唯一编号之类的东西Point?并有这样的东西:

struct cCompare{
    bool operator()(const Point &p1, const Point &p2){
         return (p1.index < p2.index);
    }
};
Run Code Online (Sandbox Code Playgroud)

c++ stdset

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

std::set&lt;classtype&gt;.find(element) 是否使用类中的 == 运算符来比较元素?

说我有

std::set<classtype> set;
class classtype {
    bool operator==(const classtype& ct) {
        //..
    } 
};
//..
std::set<classtype>::iterator it = set.find(element);
Run Code Online (Sandbox Code Playgroud)

Find 使用类中的 == 运算符是否正确?

我的参考文献还说它有 log(n) 最坏情况运行时间,其中 n 是集合中元素的数量。这在内部是如何实现的呢?我知道关键是集合中的元素有一个顺序(因此插入需要很长时间才能创建该顺序),对于整数集,顺序意味着什么很清楚,但对于随机类则不然。

c++ time-complexity stdset

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

在std :: set中查找元素

鉴于我有一个std::set,我如何找出该集合中的一个元素是否在另一个元素之前.例如,像这样的东西 -

bool before(const std::set &s, const int first, const int second) const {
  auto it_first = s.find(first);
  auto it_second = s.find(second);
  return it_first <= it_second;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用,因为<=没有为双向迭代器定义,但是如何做这样的事情呢?

c++ stl stdset c++11

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

如何正确释放一组指针?

如果我有一个容器,比如std::set指向动态对象的指针,那么我如何释放它的元素?

int main()
{

    // new scope
    {
        int x = 10;
        std::set<int*> spi;
        spi.insert(new int(1));// elem is a dynamic object init from 1
        spi.insert(new int[3]()); // elem is a dynamic array of 3 default-init integers
        spi.insert(&x); // elem is address of stack memory object
    }
}
Run Code Online (Sandbox Code Playgroud)

那么如何有效地释放具有动态内存的元素呢?

  • 我知道我可以使用一组 shared_ptrs 或 unique_ptr 但为了练习,我想知道如何使用。

c++ dynamic-memory-allocation stdset

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

C2676:二进制“&lt;”:“const _Ty”未定义此运算符或转换为预定义运算符可接受的类型

对于下面的代码,我不断收到此错误。

在阅读,我相信我的错误是it++在我的for循环,我试图与更换next(it, 1),但它并没有解决我的问题。

我的问题是,迭代器是给我问题的那个吗?

#include <iostream>
#include <vector>
#include <stack>
#include <set>
using namespace std;

struct Node
{
    char vertex;
    set<char> adjacent;
};


class Graph
{
public:
    Graph() {};
    ~Graph() {};

    void addEdge(char a, char b)
    {
        Node newV;
        set<char> temp;
        set<Node>::iterator n;

        if (inGraph(a) && !inGraph(b)) {
            for (it = nodes.begin(); it != nodes.end(); it++)
            {
                if (it->vertex == a)
                {
                    temp = it->adjacent;
                    temp.insert(b);
                    newV.vertex = b;
                    nodes.insert(newV);
                    n = nodes.find(newV);
                    temp …
Run Code Online (Sandbox Code Playgroud)

c++ iterator graph c++-standard-library stdset

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

std:hash 可以访问类的私有成员

我想散列一个具有两个私有成员的类,例如:

foo.h

class Foo {
    private:
        std::string a;
        std::string b;

    public:
        Foo (std::string a, std::string b);
        bool operator==(const Foo& other) const;
        bool operator!=(const Foo& other) const;
        std::size_t operator()(const Foo& ) const;
};

namespace std {
    template <> struct hash<Foo> {
        std::size_t operator()(const Foo& cp) const;
    };
}
Run Code Online (Sandbox Code Playgroud)

文件

Foo::Foo (std::string _a, std::string _b) {
    this->a = _a;
    this->b = _b;
}

bool Foo::operator== (const Foo& other) const {
    return this->a == other.a && this->b == other.b;
}

bool Foo::operator!= (const …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap stdset c++11 stdhash

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

如何在读取重复值时删除它们?

MRE(因为许可证问题):

std::set<std::string> pool;

// values: 10 10 1 3 4 3 3 2 5 7 5 4 3 9 8 8 7
// (values is an iterable collection)
for (const auto& value : values) {
    pool.insert(value);
}

// expected read: 10 1 3 4 2 5 7 9 8

// actual read: 1 10 2 3 4 5 7 8 9 (10 after 1 because std::string)
Run Code Online (Sandbox Code Playgroud)

我应该使用什么集合来实现这一目标?

c++ stdset

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