我应该如何将带有迭代器的 std::list(1) 声明为 std::map,它将 std::string 映射到 std::list (1) 的迭代器?是否可以?
std::list<std::map<std::string, (1) ???>::iterator>;
std::map<std::string, (1) ???::iterator>;
Run Code Online (Sandbox Code Playgroud)
我想要这个的原因 - FIFO 队列能够通过键快速删除。
一种可能的解决方案:
struct decl_t {
typedef std::map< std::string, decl_t > map_t;
typedef std::list< std::pair< int, typename map_t::iterator > > list_t;
list_t::iterator it;
};
Run Code Online (Sandbox Code Playgroud) 我想既为控制块和预分配存储器value_type
用于shared_ptr
在一个堆请求(像std::make_shared
),但不要在它立即构造的任何对象。当我实际上需要构造对象时,请使用 position new
。是否可以?两者都没有std::make_shared
或std::allocate_shared
似乎没有解决我的问题。
读取铬代码,找到了有用的宏来处理POSIX兼容系统上系统调用的EINTR错误.这是代码(base/posix/eintr_wrapper.h):
#define HANDLE_EINTR(x) ({ \
decltype(x) eintr_wrapper_result; \
do { \
eintr_wrapper_result = (x); \
} while (eintr_wrapper_result == -1 && errno == EINTR); \
eintr_wrapper_result; \
})
Run Code Online (Sandbox Code Playgroud)
问题是最后一个陈述在宏观中的作用是什么eintr_wrapper_result;
?如果我们使用逗号而不是分号 - 很明显 - 返回上一个操作的结果(逗号运算符).但是这个案子的目的是什么?
在Bruce Eckel的"Thinking in C++"的帮助下学习C++,坚持练习32,第10章.问题是如何更改链接顺序,Mirror :: test()调用对象m5返回false.这是我的代码.
#ifndef MIRROR_H_
#define MIRROR_H_
class Mirror {
public:
Mirror() {logic_ = true; self_ = 0;};
Mirror(Mirror *ptr) {self_ = ptr; logic_ = false;};
bool test() {
if (self_ != 0) {
return self_->test();
} else {
return logic_;
}
};
private:
bool logic_;
Mirror *self_;
};
#endif // MIRROR_H_
Run Code Online (Sandbox Code Playgroud)
任务
#include "mirror.h"
Mirror m1;
Run Code Online (Sandbox Code Playgroud)
#include "mirror.h"
extern Mirror m1;
Mirror m2 (&m1);
Run Code Online (Sandbox Code Playgroud)
#include "mirror.h"
extern Mirror m2;
Mirror m3 (&m2); …
Run Code Online (Sandbox Code Playgroud) 我的C++代码编译并运行,但没有输出打印到控制台.我认为它与字符串变量有关,但我不确定.我是一个总菜鸟,任何帮助将不胜感激.我正在使用GNU GCC编译器的代码块.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string botlong, botshort, secondline;
botlong = "bottles of beer on the wall,";
botshort = "bottles of beer";
secondline = "Take one down and pass it around,";
for(int bottles = 99; bottles<=0; bottles--)
{
cout<<bottles <<botlong <<bottles <<botshort;
for(int lostB = 98; lostB<=0; lostB--)
{
cout<<secondline<<lostB<<botlong;
}
}
return 0;
};
Run Code Online (Sandbox Code Playgroud)