如果我想构建一个非常简单的数组
int myArray[3] = {1,2,3};
Run Code Online (Sandbox Code Playgroud)
我应该用std::array吗?
std::array<int, 3> a = {{1, 2, 3}};
Run Code Online (Sandbox Code Playgroud)
使用std :: array比使用常规的有什么好处?性能更高吗?更容易处理复制/访问?
对于下面的代码,我收到错误
#include <iostream>
#include <functional>
using namespace std;
class A {};
class B {};
namespace N
{
    void func(A a, int z){}
    void func(B a, int z){}
}
void func_1(std::function<void(A, int)> x)
{
    A a;
    x(a, 1);
}
int main()
{
    func_1(N::func);
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
    main.cpp:23:19: error: cannot resolve overloaded function 'func' based on conversion to type 'std::function<void(A, int)>'
   23 |     func_1(N::func);
Run Code Online (Sandbox Code Playgroud)
如果我们对func_1(N::func);as进行静态转换func_1(static_cast<void (&)(A, int)>(N::func));,那么这个工作正常。但我希望这能在没有演员的情况下工作。
与c ++ 11一样,我们有两种类型的列表:
std::list<int> lst = { 1, 2, 3, 4, 5 };
std::forward_list<int> flst = { 5, 4, 3, 2, 1};
Run Code Online (Sandbox Code Playgroud)
我们知道该列表基于双向链表,而forward_list基于单链表.
我们该如何决定使用哪一个?其他列表中是否有任何性能优势?
我在比较班级。对于下面的代码
#include <string>
#include <set>
#include <tuple>
#include <cassert>
enum class e : bool
{
    positive = true,
    negetive = false
};
class A
{
public:
    int a;
    e e1 : 1;
    
  friend bool operator==(const A&, const A&);
};
 
 bool operator==(const A& lhs, const A& rhs) {
  auto tie = [](const A& a1) {
    return std::tie(a1.a, a1.e1);
  };
  auto x1 = tie(lhs);
  auto x2 = tie(rhs);
  return x1 == x2;   
}
int main()
{
A a1;
a1.a = 10;
a1.e1 …Run Code Online (Sandbox Code Playgroud) 根据一些书,类中定义的函数(连同标题中的定义)始终是内联的。真的吗?
我们如何使用测试应用程序创建这样的场景?
我有以下简单的代码,我还没有找到一个线程,其中智能指针用于这个简单的情况,而是复制对象:
int main()
{
    int i = 1;
    std::unique_ptr<int> p1(&i);
    *p1 = 2;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
_BLOCK_TYPE_IS_INVALID当指针超出范围时,会导致这种情况.如果我打电话p1.release(),代码工作正常,我不会得到这个错误.我认为这样的指针很聪明,可以处理悬空指针?
另一种方法是,如果我有i的副本,则不会出现上述错误:
std::unique_ptr<int> p1(new int(i));
Run Code Online (Sandbox Code Playgroud)
在这里使用智能指针有什么好处,我不想执行本地副本?
如果我要使用原始指针:
int i = 1;
int *p1 = &i;
*p1 = 2;
Run Code Online (Sandbox Code Playgroud)
即使我不编码,我也不会收到错误:
p1 = nullptr;
Run Code Online (Sandbox Code Playgroud) 我有vector包含堆上分配的对象.我想从向量中删除一些元素.哪个是从vector中删除元素并删除它的最佳方法.
在以下代码中尝试了一种方法:
class MyObj
{
    bool rem;
public:
    MyObj(bool r) : rem(r) { cout << "MyObj" << endl; }
    ~MyObj() { cout << "~MyObj" << endl; }
    bool shouldRemove() const noexcept { return rem; }
};
int main()
{
    vector<MyObj*> objs;
    objs.push_back(new MyObj(false));
    objs.push_back(new MyObj(true));
    objs.push_back(new MyObj(false));
    objs.push_back(new MyObj(true));
    auto itr =  objs.begin();
    while (itr != objs.end())
    {
        if ((*itr)->shouldRemove())
        {
            delete *itr;
            *itr = nullptr;
            itr = objs.erase(itr);
        }
        else
            ++itr;
    }
    // size will be two
    cout << …Run Code Online (Sandbox Code Playgroud) 有很多关于清理的问题和答案std::vector,我想知道为什么,在我读过的所有内容中,没有人只是说:
existingVector = std::vector<whatever>();
这不是清除矢量的简单方法吗?
这似乎是一种避免unique_ptr使用指向unique_ptr对象的指针的简单方法.这并不困难.所以使用unique_ptr是一种绅士的协议,而不是真正超强的?
#include <iostream>
#include <memory>
using namespace std;
class Box {
  public:
    int num;
};
void update_box(unique_ptr<Box>* pu);
int main(){
  unique_ptr<Box> b{new Box};
  unique_ptr<Box>* u = &b;
  update_box(u);
  cout << b->num << endl; // Outputs 99.
  return 0;
}
void update_box(unique_ptr<Box>* pu) {
  (*pu)->num = 99;
}
Run Code Online (Sandbox Code Playgroud) 借助 c++11 的新特性,我们可以进行 In-class 成员初始化。但是仍然不能在类中定义静态数据成员。
class A
{
    static const int i = 10;
    int j = 10;
    const int k = 20;
    static int m = 10; // error: non-const static data member must be initialized out of line
};
Run Code Online (Sandbox Code Playgroud)
为什么不提供此功能?
由于内联函数将替换代码中的实际调用,因此将内联函数作为const调用有什么用。
Inline void adddata() const {...}
Run Code Online (Sandbox Code Playgroud) 分段故障
include <iostream>
using namespace std;
int main()
{
    int a,b[a],total=0;
    cin>>a;
    for(int i=0;i<a;i++){
        cin>>b[i];
        total=total+b[i];
    }
    cout<<total<<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
请帮我看看为什么这段代码是分段错误
c++ ×12
c++11 ×4
unique-ptr ×2
vector ×2
forward-list ×1
heap-memory ×1
list ×1
static ×1
std-tie ×1
stdarray ×1
stl ×1