小编Jes*_*uhl的帖子


如何在 C++ 中的预定义范围内循环向量

是否可以根据预定义的范围循环向量?

我需要这样的东西:

for(auto& it : myvector[0, 30])
Run Code Online (Sandbox Code Playgroud)

或者:

for(auto& it : myvector[0-30])
Run Code Online (Sandbox Code Playgroud)

这可能吗?我已经尝试了两种方法,但都不起作用。

c++

4
推荐指数
1
解决办法
216
查看次数

auto和delctype(auto)类型演绎示例

我读了一篇关于auto类型演绎的文章decltype,我想知道我的逻辑是否正确如何在下面的例子中推断出类型(所以如果我弄错了请纠正我:)

#include <iostream>
using namespace std;

class Widget
{
public:
    Widget() = default;
};

int main()
{
    Widget w;
    const Widget& cw = w;          // cw is const Widget&
    auto myWidget1 = cw;           // (1) myWidget1  is Widget
    decltype(auto) myWidget2 = cw; // (2) myWidget2 is const Widget&
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我所理解的是:

for 1:使用自动类型推导,在这种情况下,它类似于通过值传递的parms的temlpate类型推导.这意味着忽略了cv-qualifiers和refs,这将导致Widget最终的类型.

for 2:使用decltype,然后传递给auto,这实际上是cw一个const Widget&然后都设置了,类型是const Widget&.

我写的/理解是对还是错?

谢谢

c++ decltype auto

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

为什么C++ to_string函数不支持char和unsigned char类型?

在cppreference.com上,它说该to_string函数可以通过以下方式工作。为什么没有char输入unsigned char

to_string(int value);
to_string(long value);
to_string(long long value);
to_string(unsigned value);
to_string(unsigned long value);
to_string(unsigned long long value);
to_string(float value);
to_string(double value);
to_string(long double value);
Run Code Online (Sandbox Code Playgroud)

c++

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

C++没有包含<string>标头的成功编译

我注意到我设法编译了以下代码而没有包含<string>.它不应该给我一个错误吗?

#include <iostream>
 using namespace std;

struct Sales_data {
    string book_no;
    double units_sold;
    double price;
};

int main() {
    Sales_data book1, book2;
    cout << "Please enter first transaction in format: ISBN, units sold & price" << endl;
    cin >> book1.book_no >> book1.units_sold >> book1.price;
    cout << "Please enter second transaction in format: ISBN, units sold & price" << endl;
    cin >> book2.book_no >> book2.units_sold >> book2.price;

    cout << "******************************" << endl;
    cout << "Total units sold = " …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

Prevent the application from crashing if one of the threads crashed

Is there any way to prevent main thread from crashing?
I want the main thread to keep running after this memory access violation exception happens.

std::thread {
    []() {
        for (auto i = 0; i < 10; ++i) {
            std::cout << "Hello World from detached thread!\n";
            std::this_thread::sleep_for(std::chrono::milliseconds(500));
        }
        std::cout << "Boom!\n";
        *(int*)(0) = 42;
    }
}.detach();

while (true) {
    std::cout << "Hello World!\n";
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading

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

nlohmann json:为什么我收到解析错误?

我真的很困惑为什么当我尝试使用 nlohmann 库解析 C++ 中的文件时会出现解析错误。

我的代码与我从他们的 GitHub 页面复制的代码完全相同:

using json = nlohmann::json;

int main() {
    ifstream ifs("test.json");

    if (nlohmann::json::accept(ifs))
        cout << "accepted" << endl;
    else
        cout << "not accepted" << endl;

    json j = json::parse(ifs);
}
Run Code Online (Sandbox Code Playgroud)

JSON 文件就是这样:

{ 
   "test": 5
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,当我到达该parse()函数时,这会引发错误,尽管accept()返回 true,这意味着 JSON 被接受为有效,但当

另外,当我做这样的事情时:

json j = json::parse(R"({"test": "aaa"})");
Run Code Online (Sandbox Code Playgroud)

效果很好。但我无法解析ifstream对象。

有谁知道这里可能存在什么问题吗?

我没有任何想法,我也毫无头绪,因为看起来我做的一切都是对的。

c++ json nlohmann-json

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

继承错误的构造函数调用

我正在实施这个钻石继承:

class Object {
private:
    int id; string name;
public:
    Object(){};
    Object(int i, string n){name = n; id = i;};
};


class Button: virtual public Object {
private: 
    string type1;
    int x_coord, y_coord;
public:
    Button():Object(){};
    Button(int i, string n, string ty, int x, int y):Object(i, n){
          type = ty;
          x_coord = x;
          y_coord = y;};
};


class Action: virtual public Object {
private:
    string type2;
public:
    Action():Object(){};
    Action(int i, string n, string t):Object(i, n){ type2 = t;};
};


class ActionButton: …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance class multiple-inheritance c++11

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

isdigit()函数传递一个中文参数

当我尝试使用isdigit()带有中文字符的函数时,它会在Visual Studio 2013中以调试模式报告一个断言,但在发布模式下没有问题.

我想如果这个函数是确定参数是否为数字,那么如果中文错误,为什么它不返回0?

这是我的代码:

string testString = "abcdefg12345??";
int count = 0;
for (const auto &c : testString) {
    if (isdigit(c)) {
        ++count;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是断言:

图片

c++

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

如何检查向量是否在指定索引内包含特定字符?

我想知道如何(如果可能的话)从向量的开头到指定的索引(如果存在字符)进行搜索。

伪代码:

vector<char> a{a, b, c, d, e, f, /*...*/};

if (from a.begin() to a[2] b exists) {
... }
Run Code Online (Sandbox Code Playgroud)

最有效的方法是什么?

c++ algorithm search vector

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