我有一个函数,它emplace()在std无序地图容器上调用方法,我需要返回emplace()调用给出的确切返回值.我知道它返回一个std::pair迭代器(无论是新的还是旧的取决于成功的操作)和一个代表成功的bool.之前我只返回了bool部分,如下所示:
auto result = map.emplace(std::make_pair(id, someObj));
return result.second;
Run Code Online (Sandbox Code Playgroud)
我知道返回类型是bool.但是现在我需要返回原始result变量,但我不知道它的类型,那么我应该在其类中放入函数原型?
我尝试typeid在迭代器上使用方法,但它给出了:
St4pairINSt8__detail14_Node_iteratorIS_IKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_ELb0ELb1EEEbE
Run Code Online (Sandbox Code Playgroud)
作为输出.
我目前正在学习c ++中的模板元编程,并偶然发现变量模板.作为一个有趣的练习,我决定使用以下用法实现编译时静态数组 -
my_array<1,2,3,4> arr; // gives an array with 4 members = 1,2,3,4
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几次迭代尝试,一路上删除语法错误,但现在我卡住了,因为没有编译器提供有用的警告.这是我目前的代码 -
#include <iostream>
template<size_t... Enteries>
constexpr size_t my_array[sizeof...(Enteries)] = {Enteries...};
int main() {
my_array<1,2,3,4,5,6> arr;
}
Run Code Online (Sandbox Code Playgroud)
但目前它与clang给出以下错误 -
static_array.cpp:7:10: error: expected ';' after expression
my_array<1,2,3,4,5,6> arr;
^
;
static_array.cpp:7:24: error: use of undeclared identifier 'arr'
my_array<1,2,3,4,5,6> arr;
^
static_array.cpp:7:2: warning: expression result unused [-Wunused-value]
my_array<1,2,3,4,5,6> arr;
^~~~~~~~~~~~~~~~~~~~~
1 warning and 2 errors generated.
Run Code Online (Sandbox Code Playgroud)
和gcc -
static_array.cpp: In function ‘int main()’:
static_array.cpp:7:24: error: expected ‘;’ before …Run Code Online (Sandbox Code Playgroud) c++ templates template-meta-programming variadic-templates c++14
我正在尝试通过在其文档的帮助下创建小程序来学习libuv.请注意,我将通过c++语言使用它,而不是c.这是我开始的 -
#include <iostream>
#include <uv.h>
int main() {
uv_loop_t *loop = malloc(sizeof(uv_loop_t));
uv_loop_init(loop);
std::cout << "Running loop" << std::endl;
uv_run(loop, UV_RUN_DEFAULT);
uv_loop_close(loop);
free(loop);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并编译它 - g++ -std=c++14 -luv main.cpp应该a.out作为输出文件,但它失败并出现错误 -
无效转换为'void*'到'uv_loop_t*{aka uv_loop_s*}
这指向malloc的使用.我通过用旧的libuv方法替换那些行来确认它 -
uv_loop_t* loop = uv_loop_new();
...
uv_loop_delete(loop);
Run Code Online (Sandbox Code Playgroud)
这不应该工作,但令人惊讶的是工作和编译在这里很好.
但我认为不是躲避这个问题,而是应该用c ++中的好方法解决这个问题.所以在这里我要求c ++中的一个很好的替代品(可能没有malloc /手动内存管理),所以我可以继续前进.
我正在尝试用.then()javascript 实现具有可链接功能的简单promise类。这是我到目前为止所做的-
class APromise {
constructor(Fn) {
this.value = null;
Fn(resolved => { this.value = resolved; });
}
then(fn) {
fn(this.value);
return this;
}
}
function myFun() {
return new APromise(resolve => {
// just resolve('Hello'); works but this doesn't
setTimeout(() => { resolve('Hello'); }, 2000);
});
}
const log = v => { console.log(v); };
myFun().then(log).then(log);
Run Code Online (Sandbox Code Playgroud)
输出-
null
null
Run Code Online (Sandbox Code Playgroud)
Instead of 'Hello' 2 times. I think it is currently ignoring setTimeout() call, how should I make this …
我要确保在施工期间弦线构件的尺寸是固定的.但是,他们可以在构建完成后增加它们的大小,然后在程序中,用户选择向其添加更多文本.
class A {
std::string name;
std::string desc;
int num;
public:
A(int, std::string, std::string);
.....
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我这样做 -
A(int n, std::string name, std::string d) :
num(n),
name(name),
desc(d)
{
....
}
Run Code Online (Sandbox Code Playgroud)
我将不得不resize()在构造函数体内调用.这意味着在构造期间的某个时间存在完整的弦长.假设我希望初始大小为5,并且用户传递20个字符串,在构造期间和之前resize(),字符串将包含20个字符,对吧?
现在我正在考虑这样做 -
A(int n, std::string name, std::string d) :
num(n)
{
name = name;
desc = d;
name.resize(5);
desc.resize(5);
....
}
Run Code Online (Sandbox Code Playgroud)
但这会是一样的吗?
我应该调用resize()传递的参数本身吗?还是有更好的方法..
我的代码如下 -
int main(){
....
auto time = std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
....
return 0;
}
Run Code Online (Sandbox Code Playgroud)
time这里的变量l与typeid().name()方法一样给出输出,但是可以安全地假设如果我用long类型替换auto ,变量仍会在不同的机器上存储正确的毫秒数吗?
我需要它,因为我不能auto在类成员中指定类型,因为它们不是constexpr或静态的,它可能是可能的.我的目的是将数据发送到我可以做的浏览器,var d = new Date(time)并显示正确的时间.通信部分已通过json格式计算出来,我只是坚持如何在不同的系统中正确存储它.
我应该如何继续将std::chrono::minutes::rep类型值转换为小时表示.
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
int main() {
minutes::rep time = 4;
std::cout << time; // outputs as minutes, need in hours
// duration cast doesn't seems to work here because it
// needs minutes instead of minutes::rep probably
return 0;
}
Run Code Online (Sandbox Code Playgroud) 使用Berkeley套接字api时,通过read/send或write/recv调用发送的内容的数据类型是什么?例如 -
char *msg = "Our Message!";
int len, bytes_sent;
len = strlen(msg);
bytes_sent = send(sockfd, msg, len, 0);
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我们使用char类型,但是我们仅限于char类型,因为send/write/sendto通常采用void *类型.我也看到了一些参数,比如我们发送一些int,它实际上可能存储在little endian/big endian中,如果它们的endianess不匹配,会导致b/w source/dest出现问题.那么为什么不char输入这个问题呢?
另外C和C++等不同的语言也有不同的大小char,为什么这不是一个问题呢?如果socket不关心任何type内容并且只是将内容视为缓冲区,那么当不同的tcp服务器/客户端用不同的语言编写并相互通信时,为什么我们不会看到数据的随机损坏?
简而言之,我可以通过套接字安全地发送什么值(类型)?
我是否必须提供两个不同版本的运算符==重载,以便无论表达式的LHS和RHS变体如何都可以运行.例如.
Class A {
...
bool operator==(int const& L, A const& R);
bool operator==(A const& L, int const& R);
...
};
Run Code Online (Sandbox Code Playgroud)
用于 -
A a;
int x = 8;
if( a == 5 || x == a){
...
}
Run Code Online (Sandbox Code Playgroud)
为什么需要这样做?不是L == R== R == L?
c++ ×8
c++11 ×6
c++14 ×3
c++-chrono ×2
date ×2
linux ×2
c ×1
constructor ×1
ecmascript-6 ×1
javascript ×1
libuv ×1
networking ×1
node.js ×1
pointers ×1
promise ×1
sockets ×1
string ×1
templates ×1