小编sur*_*h m的帖子

GSL断言与C ++中的断言的优势?

我知道assertC ++中的用法。想知道有没有之间任何利益差别(我认为assert根据昂贵,如提到的https://www.learncpp.com/cpp-tutorial/7-12a-assert-and-static_assert/明智这样的表现,都同样吗?)在使用gsl_assertover assert吗?为什么要gsl_assert在gsl库中添加代码,因为assertc ++ 已经提供了支持(即使assert来自“ C”,因为我们添加#include<cassert>assert在C ++中的使用)?

#include <iostream>
#include <gsl/gsl_assert>
using namespace std;

int main()
{
    int val;
    cin >> val;
    Ensures( val > 5 );
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ guideline-support-library

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

如何使用auto关键字在C++中分配类型为uint32_t或uint64_t的变量

考虑auto var = 5u;.在这里,我使用后缀u,因此var将被推断为unsigned int.有没有办法实现类似的东西uint32_tuint64_t类型?C++ 11或C++ 14中有后缀吗?

c++11

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

是否在std :: string以外的任何其他标准库容器中使用?

可以使用SSO(小/短字符串优化)std::string.但它是否允许用于其他标准库容器(例如std::vector)?答案取决于容器的模板参数是内置类型还是用户定义类型?

c++ stl c++11 c++14

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

如何从C++中的值<bool>构造整数值

#include <iostream>
#include <vector>

int main()
{
    std::vector<bool> bitvec{true, false, true, false, true};
    std::string str;
    for(size_t i = 0; i < bitvec.size(); ++i)
    {   
        // str += bitvec[i];
        std::vector<bool>::reference ref = bitvec[i];
        // str += ref;
        std::cout << "bitvec[" << i << "] : " << bitvec[i] << '\n';
        std::cout << "str[" << i << "] : " << str[i] << '\n';
    }   
    std::cout << "str : " << str << '\n';
}
Run Code Online (Sandbox Code Playgroud)

我们如何从bool值的std :: vector构造一个整数值.我想将它转换为std :: string然后转换为bool值的std :: vector中的整数,但是将其转换为bool值的std …

c++ c++11 c++14

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

遍历列表容器,排除最后一个元素

我需要遍历一个std::list直到end() - 1(所以我不希望在遍历时包含最后一个元素).有效的方法是什么?

#include <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> l{1,2,3,4,5};

    for(auto itr = l.begin(); itr != l.end() - 1; ++itr)
    {   
        cout << *itr << '\n';
    }   
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++14

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

执行memset时出现Seg Fault

在运行以下程序时,我遇到了段错误.但如果我评论memset,我不会得到seg错误.无法弄清楚原因.如果我需要清除结构内容(例如值),我们可以做什么而不是memset?

#include <iostream>
#include <cstring>
#include <map>
using namespace std;

struct test
{
    int value;
    map<int, int> m;
};

void print(test *ptr)
{
    cout << "map size() : " << ptr->m.size();
    for(auto itr = ptr->m.begin(); itr != ptr->m.end(); ++itr)
    {   
    }   
}

int main()
{
    test obj;
    memset(&obj, 0, sizeof(obj));
    print(&obj);
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

标签 统计

c++ ×5

c++11 ×5

c++14 ×3

guideline-support-library ×1

stl ×1