小编csg*_*guy的帖子

包含守卫在名称空间之间是否应该是唯一的?

我在两个名称空间中使用相同的类名,比如A和B.在包含不同命名空间的类时,包含警卫是否应该是唯一的?

我的意思是不能有两个文件名称AFile.h(在不同的目录中)具有相同的包含警卫和声明不同的命名空间?

档案1:

#ifndef AFILE_H

#define AFILE_H

命名空间A {

CAFile类

{...

};

};

#万一

文件2:

#ifndef AFILE_H

#define AFILE_H

命名空间B {

CAFile类

{...

};

};

#万一

c++ namespaces include-guards include

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

解释数组文档(C ++)

根据https://en.cppreference.com/w/cpp/language/default_initialization

“如果T是数组类型,则数组的每个元素都将默认初始化”

我是否误解了,因为我们都知道 http://www.cplusplus.com/doc/tutorial/arrays/

默认情况下,局部范围的常规数组(例如,在函数中声明的常规数组)未初始化。这意味着其元素均未设置为任何特定值。在声明数组时,它们的内容尚未确定。

...

初始化程序甚至可以没有任何值,只需括号即可:这将创建一个包含五个int值的数组,每个int值均初始化为零

第一个来源如何准确?在哪里可以找到更多可信的文档来解决阵列默认初始化的这种现象?

c++ arrays

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

对象和实例的定义

根据什么是C ++中的类和对象?

一个类就像一个蓝图,一个对象就像是用该蓝图建造的房屋。

您可以拥有许多具有相同布局/平面图的房屋(读取类),但每个房屋都是其自己的实例(读取对象)。每个人都有自己的所有者,家具等。

Note that there are also objects whose blueprint is not a class (e.g. integers).

In summary, there are different types of object (e.g. int, float, etc.). You can create user-defined types, called 'classes'.

In C++, the term object is used for instances of any type, including classes and fundamental types.

However, according to http://www.cplusplus.com/doc/tutorial/classes/

An object is an instantiation of a class. In terms of variables, a class would be the type, and an object …

c++ class definition object object-lifetime

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

`iterator` 和 `const_iterator` 不是 STL 容器的必需成员吗?

摘自了解迭代器/const_iterator 实现

“虽然iteratorconst_iterator是在 范围内声明的类型vector,但不要求vector(或任何 STL 容器)具有任一类型的成员 -iterator并且const_iterator是接口的一部分, std::vector例如该成员的重载begin()返回这些类型,但没有说明这些函数如何获得它们返回的迭代器”

此外,STL 容器必须具有:

“返回迭代器的开始和结束函数”

例如,上面指出iterator并且const_iterator不是 STL 容器的必需成员vector。我认为这意味着返回的类型.begin.end将根据实现而有所不同。

所以我想知道为什么这没有问题,因为我看到很多人写出std::vector<someType>::iteratorstd::vector<someType>::const_iterator在何处iteratorconst_iterator被指定而不是使用auto例如:

for (std::vector<int>::iterator i = s.begin(); i != s.end(); i++)
{

}
Run Code Online (Sandbox Code Playgroud)

c++ containers iterator stl

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

C++ 默认复制构造函数不可行

我有 2 个课程,User,和BBoard(公告板)。

我有一个成员函数BBoard,它检查从文件中读取的数据并将其推回向量userList。我还有一个成员函数来检查 a 是否User存在(如果User存在,我想使用默认的复制构造函数并将其分配给currentUser

这些是我在 BBoard 中的私有变量:

// private:
//   std::string title;
//   std::vector<User> userList;
//   User currentUser;
//   std::vector<Message> messageList;
Run Code Online (Sandbox Code Playgroud)

BBoard这是推回s的成员函数User

while (inFS >> dataName && inFS >> dataPass) {
  User x(dataName, dataPass);
  userList.push_back(x);
}
Run Code Online (Sandbox Code Playgroud)

BBoard这是检查 a 是否User存在并尝试使用默认复制构造函数将其分配给 的成员函数currentUser

bool BBoard::userExists(const string& uName, const string& uPass) const {
  for (int i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor

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

为什么BFS的复杂度是O(V+E)而不是O(E)?

这是一个通用的 BFS 实现:

在此输入图像描述V对于具有节点和总边数的 连通图E,我们知道每条边在内循环中都会被考虑两次。那么,如果 BFS内循环的迭代总数为2 * number of edges E,那么运行时间不是会是O(E)吗?

algorithm big-o breadth-first-search time-complexity depth-first-search

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

Are int* and int** different types? C++

I was curious as to how a pointer to a pointer variable could tell whether it is given just a regular pointer variable or a pointer to a pointer.

I tried this code below and got this error:

int x = 3;
int** y = &x;
Run Code Online (Sandbox Code Playgroud)

prog.cpp:7:12: error: invalid conversion from ‘int*’ to ‘int**’ [-fpermissive]

Is it correct to say &x is an int* type which is different from an int** type and so on?

c++ pointers

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

模数行为 -1 % 3 (C++)

if (currIndex < 0) {

            cout << currIndex << " % " << array.size() << endl;

            currIndex = currIndex % array.size();

            cout << currIndex << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:

-1 % 3
0
Run Code Online (Sandbox Code Playgroud)

-1 % 3 = -1 在 C++ 中,为什么返回 0?

完整片段:https : //ideone.com/leWqhi

c++ modulo

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