小编Abh*_*del的帖子

返回一个空字符串:c ++中的有效方法

我有两种从函数返回空字符串的方法.

1)

std::string get_string()
{
   return "";
}
Run Code Online (Sandbox Code Playgroud)

2)

std::string get_string()
{
   return std::string();
}
Run Code Online (Sandbox Code Playgroud)

哪一个更有效率,为什么?

c++ string performance

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

Json Cpp isMember()总是返回True

例如在简单的json中

{
    "A" :
    {
       "B" :
       {
         --something--
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

第一个案例:

json::Value root;
const Json::Value x = root["A"]["B"];
if (root.isMember("A")) --- always returns TRUE..
Run Code Online (Sandbox Code Playgroud)

第二种情况:

Json::Value root;
If (root.isMember("A"))  ---- works fine
const Json::Value x = root["A"]["B"]; 
Run Code Online (Sandbox Code Playgroud)

知道First Case有什么问题吗?即使我xisMember()电话前得到了.

c++ json

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

是否需要包含头文件?

添加.h:

int add(int x, int y); // function prototype for add.h -- don't forget the semicolon!
Run Code Online (Sandbox Code Playgroud)

为了在 main.cpp 中使用这个头文件,我们必须 #include 它(使用引号,而不是尖括号)。

主要.cpp:

#include <iostream>
#include "add.h" // Insert contents of add.h at this point.  Note use of double quotes here.

int main()
{
    std::cout << "The sum of 3 and 4 is " << add(3, 4) << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

添加.cpp:

#include "add.h"

int add(int x, int y)
{
    return x + y;
}
Run Code Online (Sandbox Code Playgroud)

嗨,我想知道,为什么我们在文件 add.cpp 中有#include“add.h”?我认为没有必要。

c++

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

标签 统计

c++ ×3

json ×1

performance ×1

string ×1