我正在尝试使用迭代器来完成一个集合,然后对该集合的成员做一些事情(如果有的话).问题在于,通常这是有效的,但有时,它会比较空集的开头和结尾,并发现它们不相等.
感兴趣的代码段是:
for(int i=0;i<input_data.num_particles();i++)
{
//loop through pairs contained in particle i's Verlet list
set<int>::iterator iter;
for(iter=verlet_vars.verlet()[i].begin();iter!=verlet_vars.verlet()[i].end();iter++)
{
//call the force() function to calculate the force between the particles
force(particles.getpart(i),particles.getpart(*iter),input_data,*iter);
}
}
Run Code Online (Sandbox Code Playgroud)
有时,即使verlet_vars.verlet()[i]中包含的集合为空,程序也会将迭代器与集合的末尾进行比较并发现它们不相等,因此它进入内部循环(最终导致程序崩溃)通过尝试调用force()函数).奇怪的是,如果我在调用内部循环之前对迭代器做了什么,比如做类似的事情:
iter=verlet_vars.verlet()[i].begin();
Run Code Online (Sandbox Code Playgroud)
然后,内部循环的比较总是返回true,程序正常运行.
PS命令verlet_vars.verlet()[i]调用集合的向量,因此[i]
verlet()函数:
std::vector<std::set<int> > verlet() const {return _verlet;}
Run Code Online (Sandbox Code Playgroud)
谢谢你的时间.
在C++ 03中,我想创建一个std :: set,在迭代时,首先出现一个整数,之后,我不关心什么顺序,但是我需要一个排序来确保没有重复组.例如,如果我有一组年份,并且在迭代时我想要在所有其他年份之前处理2010年.
std::set<int> years;
// I do not know the set of years up front, so cannot just make a vector, plus
// there could potentially be duplicates of the same year inserted more than
// once, but it should only appear once in the resultant set.
years.insert(2000);
years.insert(2001);
years.insert(2010);
years.insert(2011);
years.insert(2013);
for (std::set<int>::iterator itr = years.begin(); itr != years.end(); ++itr) {
process_year(*itr);
}
Run Code Online (Sandbox Code Playgroud)
基本上,我需要提供一个比较器,在运行时已知的某一年(例如2010年)与其他年份的比较少,但剩余的年份是按顺序排序的,但没有按任何必要的顺序排列,只是为了确保没有重复.组.
我想存储std::set的Point3D对象,我比较函数的定义如下(按字典顺序):
bool operator<(const Point3D &Pt1, const Point3D &Pt2)
{
const double tol = 1e-5;
if(fabs(Pt1.x() - Pt2.x()) > tol)
{
return Pt1.x() < Pt2.x();
}
else if(fabs(Pt1.y() - Pt2.y()) > tol)
{
return Pt1.y() < Pt2.y();
}
else if(fabs(Pt1.z() - Pt2.z()) > tol)
{
return Pt1.z() < Pt2.z();
}
else
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下set包含相同的点,我认为问题来自比较函数,但我找不到确切的问题.任何帮助,将不胜感激!
既然std::set是作为二叉树实现的,它如何比较std::string不等式?看起来像a < b && b < a吗?
是直接使用字符串的长度还是以某种方式对其进行散列?它是否完全保证字符串的唯一性?
我有个问题.当我使用带有自定义比较器的std :: set时,其他操作(如erase或count)无法正常工作.例如:
int sz(int const & n) {
return __builtin_popcount(n);
}
struct comp {
bool operator()(int const & a, int const & b) const {
return sz(a) >= sz(b);
}
};
void solve() {
set<int, comp> s;
for (int i = 0; i < 10; ++i)
s.insert(i);
for (int x : s)
cerr << x << " ";
cerr << "\n";
for (int i = 0; i < 10; ++i)
cerr << s.count(i) << " ";
}
Run Code Online (Sandbox Code Playgroud)
输出将是:
7 …Run Code Online (Sandbox Code Playgroud) 给定std::set,在时间迭代期间更改集合的最佳方法是什么?
例如:
std::set<T> s; // T is a some type (it's not important for the question).
// insertions to s
for (std::set<T>::iterator it = s.begin(); it != s.end(); it++) {
T saveIt(*it);
s.erase(*it);
s.insert( saveIt + saveIt ); // operator+ that defined at `T`
}
Run Code Online (Sandbox Code Playgroud)
根据我在某些来源中读到的内容,这是不好的方法,因为:从集合中删除可能会改变集合的结构.
那么更好(/最好)的方法是什么?
An std::set是一种分类的关联容器,可快速查找其元素。密钥以一种有序的方式插入,一旦插入就不能修改密钥以保持该顺序。
考虑下面的示例,该示例构造一个std::set,int*然后尝试破坏其元素的排序:
#include <iostream>
#include <set>
int values[] = { 50, 40, 30, 20, 10 };
// Comparator that sorts by pointed value
struct comparator {
bool operator()(const int* left, const int* right) const {
return *left < *right;
}
};
using my_set_t = std::set<int*, comparator>;
// Checks if each of the elements of `values` are in the set
void output(const my_set_t & foo)
{
for (auto & x : values) {
std::cout << …Run Code Online (Sandbox Code Playgroud) 我std::set用来存储类的唯一实例.std::set没有重载的下标运算符,所以你不能这样做set[0].
我找到了一种方法:
auto myClass = *std::next(set.begin(), index);
Run Code Online (Sandbox Code Playgroud)
但是,我发现一遍又一遍地复制该代码是单调的.所以我决定只扩展std::set(class sset)并且只是重载其中的下标运算符会更方便.
template <class T>
class sset: public std::set<T>
{
public:
T operator[](const uint32_t i) const
{
if(i <= (this->size()-1))
return *std::next(this->begin(), i);
else
throw std::out_of_range("Index is out of range");
}
};
int main()
{
auto myClass = set[0]; //works and no exception thrown
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我实现了所期望的行为,但我发现必须有一个原因,标准不包括下标运算符.当然不只是懒惰.
是否有任何先例不利或可能出现的未来问题?
由于std::set不insert()重复,因此可以确保包含唯一元素。使用重载时erase(const key_type&),其对象将最多包含1个相同值的元素。因此,它可能返回1(如果存在)或0(否则)。
什么时候可以erase(const key_type&)返回超过1?
换句话说,返回a size_type而不是simple 的目的是什么bool?
在下面的 C++ 调用 reset() 列表、向量、映射中,既没有错误也没有警告。
但是,当我尝试在 set 中执行此操作时,出现错误。
错误消息是 [ 没有匹配的成员函数调用 'reset' ]
为什么会这样???有人可以与社区分享您的知识吗?
std::shared_ptr<int> sp;
sp.reset(new int(11));
sp.reset();
std::map<int, std::shared_ptr<int>> my_map;
for (auto it = my_map.begin(); it != my_map.end(); ++it) {
it->second.reset();
(*it).second.reset();
}
std::list<std::shared_ptr<int>> my_list;
for (auto& x : my_list) {
x.reset();
}
std::vector<std::shared_ptr<int>> my_vec;
for (auto it = my_vec.begin(); it != my_vec.end(); ++it) {
it->reset();
(*it).reset();
}
std::set<std::shared_ptr<int>> my_set;
for (auto& x : my_set) {
x.reset(); // ERROR!!!
}
for (auto it = my_set.begin(); it != my_set.end(); …Run Code Online (Sandbox Code Playgroud) 我有以下Edge课程:
class Edge {
public:
int src, dest;
bool operator== (const Edge &edge) const {
return ((src == edge.src) && (dest == edge.dest)) || ((src == edge.dest) && (dest == edge.src));
}
bool operator<(const Edge& edge) const {
return !(((src == edge.src) && (dest == edge.dest)) || ((src == edge.dest) && (dest == edge.src)));
}
Edge(int src, int dest) {
this->src = src;
this->dest = dest;
}
};
Run Code Online (Sandbox Code Playgroud)
覆盖<运算符的要点是,当我尝试使find集合中的边Edge(0, 1)等于 时Edge(1, …
c++ ×11
stdset ×11
c++11 ×5
std ×3
c++03 ×1
class ×1
comparator ×1
invariants ×1
return-type ×1
set ×1
stdstring ×1
stdvector ×1