我有一个std::list< std::pair<std::string,double> >,我知道按照分类排序std::string element.
因为我希望做了很多std::find_if基于对std::string元素,相信std::map<string,double,MyOwnBinaryPredicate>与lower_bound和upper_bound会更充足.
事实是,我希望以有效的方式使用insert元素std::map.所以我想使用额外的迭代器来insert加快速度.
我认为,最简单的方法是使用一个const_reverse_iterator要经过std::list和使用begin()的std::map.
你会这样做,还是一个坏主意?
谢谢!
// ((++currentEntry)--) is equivalent to (currentEntry + 1). Kind of.
menuEntries.insert((++currentEntry)--, newEntries.begin(), newEntries.end());
Run Code Online (Sandbox Code Playgroud)
所以我这里有世界上最糟糕的代码.有一个更好的方法吗?
当使用'+ 1'时,我得到这个:
source/menu.cpp:146:37: error: invalid operands to binary expression
('list<menuEntry *>::iterator' (aka '_List_iterator<menuEntry *>') and
'int')
menuEntries.insert(currentEntry + 1, ...
~~~~~~~~~~~~ ^ ~
Run Code Online (Sandbox Code Playgroud) 给定一个std :: list的迭代器,如何替换迭代器引用的位置的对象?目前,我所能想到的是使用新对象和迭代器调用insert(在迭代器引用的元素之前插入新对象),然后调用erase来删除要替换的对象.是否有更少的迂回方式来完成替换?
我一直在遇到"'xxx'没有命名类型"错误很多,我之前读过的大多数帖子都提到过这个错误会出现一些依赖性问题.但是,我似乎无法找到我的.这是我得到的:
GameLib.h
#ifndef GAMELIB_H_
#define GAMELIB_H_
//Structures
struct player_t {
std::string name;
int MMR;
};
//Prototypes
void* queueUpPlayer(void*);
int randomMMR();
std::string randomName();
#endif /* GAMELIB_H_ */
Run Code Online (Sandbox Code Playgroud)
PlayerGroup.h
#ifndef GROUP_H_
#define GROUP_H_
class playerGroup {
private:
std::list<player_t> players;
std::list<player_t>::iterator it;
const int totalSize = 10;
public:
//Constructor
playerGroup();
//Destructor
~playerGroup();
//Add
void add(const player_t p);
....
};
#endif /* GROUP_H_ */
Run Code Online (Sandbox Code Playgroud)
PlayerGroup.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <list>
#include "GameLib.h"
#include "playerGroup.h"
using namespace std;
playerGroup::playerGroup() {} …Run Code Online (Sandbox Code Playgroud) 我想知道是否有任何内置或完善的方式(即通过lambda)来浏览std :: list的元素并找到所有匹配给定值的元素?我知道我可以遍历所有这些,但我想我会问是否有办法让迭代器只迭代符合给定条件的元素?我下面的示例只给出了第一个匹配元素的迭代器.
#include <list>
#include <algorithm>
#include <stdio.h>
int main()
{
std::list<int> List;
List.push_back(100);
List.push_back(200);
List.push_back(300);
List.push_back(100);
int findValue = 100;
auto it = std::find_if(List.begin(), List.end(), [findValue](const int value)
{
return (value == findValue);
});
if (it != List.end())
{
for (; it != List.end(); ++it)
{
printf("%d\n", * it);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
感谢您的任何反馈.
我正在编写一个与std :: list一起使用的自定义分配器.列表大小将始终限制为一个较小的数字,列表元素将在约束树搜索算法中非常频繁地分配和释放,因此我认为自定义池分配器(从堆栈中分配元素)应该提高性能.
我的问题是std :: list如何分配用于存储链接/节点的数据结构.它将使用自定义分配器来分配元素,但如果节点仍然从堆中分配,那么这将不会有太大帮助.
这是我正在实现的自定义分配器:
#include <algorithm>
#include <cassert>
template <class Tp, std::size_t N>
class PoolStackAllocator {
public:
typedef Tp value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template<typename U>
struct rebind {
typedef PoolStackAllocator<U, N> other;
};
inline explicit PoolStackAllocator() : data_size_(0) {}
template <class U, std::size_t M>
inline explicit PoolStackAllocator(const PoolStackAllocator<U, M>& other) : data_size_(other.data_size_) {
typename PoolStackAllocator<U>::const_pointer i = other.data_;
typename PoolStackAllocator<U>::const_pointer …Run Code Online (Sandbox Code Playgroud) class A, B;
class A {
public:
A& operator= ( const A &rhs ) { return *this; }
};
class B: public A {
public:
B& operator= ( const A &rhs ) { return *this; }
};
A a;
B b;
std::list < A > aa;
std::list < B > bb;
a = b; // works
b = a; // works
// aa = bb; // fails
// bb = aa; // fails
Run Code Online (Sandbox Code Playgroud)
如何让bb = aa工作?
请考虑以下最小示例:
#include <functional>
#include <algorithm>
#include <list>
int main() {
std::list<std::function<void()>> list;
list.push_back([&list](){ list.push_back([](){ throw; }); });
std::for_each(list.cbegin(), list.cend(), [](auto &&f) { f(); });
}
Run Code Online (Sandbox Code Playgroud)
它在运行时编译并抛出异常.
我的猜测是只有第一个lambda被执行std::for_each,但显然我错了:如果我在列表的末尾添加另一个lambda,迭代也会达到lambda.
让我们恢复示例(push_front而不是push_back和crbegin/ crend而不是cbegin/ cend):
#include <functional>
#include <algorithm>
#include <list>
int main() {
std::list<std::function<void()>> list;
list.push_front([&list](){ list.push_front([](){ throw; }); });
std::for_each(list.crbegin(), list.crend(), [](auto &&f) { f(); });
}
Run Code Online (Sandbox Code Playgroud)
由于前面的例子,我预计这也会编译和崩溃.
相反,它编译并且不会崩溃.这次,不执行推到列表前面的功能.
问题很简单:这是正确的吗?
为什么两个例子如此违反直觉?
在第一种情况下,我期待一些不同的东西,我错了,这不是问题.
无论如何,我希望两个循环之间的一致性.我的意思是,第二个函数在一个案例中执行,而在另一个案例中不执行,但我在两种情况下都是从开始到结束迭代.
我的推理出了什么问题?
注意:这不是我应该"使用list还是deque"的问题.这是一个关于迭代器在面对的有效性的问题insert().
这可能是一个简单的问题,我只是太密集了,看不到正确的方法来做到这一点.我正在实现(无论好坏)网络流量缓冲区作为a std::list<char> buf,并且我将当前的读取位置保持为迭代器readpos.
当我添加数据时,我会做类似的事情
buf.insert(buf.end(), newdata.begin(), newdata.end());
Run Code Online (Sandbox Code Playgroud)
我现在的问题是,如何保持readpos迭代器有效?如果它指向旧的中间buf,那么它应该没问题(通过std :: list的迭代器保证),但通常我可能已经读取并处理了所有数据而且我有readpos == buf.end().在插入之后,我readpos 总是希望指向下一个未读的字符,在插入的情况下应该是第一个插入的字符.
有什么建议?(没有将缓冲区更改为a std::deque<char>,这似乎更适合任务,如下所示.)
更新:从使用GCC4.4的快速测试中我发现deque和list的行为方式不同readpos = buf.end():插入结束后,readpos在列表中被破坏,但指向deque中的下一个元素.这是标准保证吗?
(根据cplusplus,任何deque :: insert()都会使所有迭代器失效.这没有用.可能使用计数器比迭代器更好地跟踪双端队列中的位置?)
已经搜索了很多网,但找不到问题的答案.
我使用reverse_iterator将值插入std :: list.虽然插入发生在预期的适当位置,但我注意到用于插入的原始reverse_iterator的值发生了变化.此外,完全不相关的reverse_iterator的值也会发生变化.我已经能够在一个简单的例子中重现这一点
#include <iostream>
#include <list>
#include <string>
int main()
{
// Creating a list of values 1, 2, 4, 5
std::list<int> myList;
myList.push_back(1);
myList.push_back(2);
myList.push_back(4);
myList.push_back(5);
// Changing it to 1, 2, 3, 4, 5 by inserting 3
std::list<int>::reverse_iterator revIter = myList.rbegin();
while(2 != *revIter)
{
std::cout << *revIter << "\t";
++revIter;
}
std::cout << "\n" << "Reverse iterator now points to " << *revIter;
// Creating a copy of the reverse Iter before inserting.
std::list<int>::reverse_iterator newRevIter …Run Code Online (Sandbox Code Playgroud)