我有一个类A,我删除了默认的构造函数.
class A {
public:
A() = delete;
A(int a): m_myInt(a) {}
private:
const int m_myInt;
};
int main () {
A foo(1); // works perfect
A bar; // won't compile
}
Run Code Online (Sandbox Code Playgroud)
如何编写良好的单元测试以确保A bar;无效?我可以编写一个不编译的测试,并将编译错误作为测试要求.我想知道,如果有更好的方法来编写单元测试?
C++有内置类型,如long long int,unsigend int.类型可以与限定符组合,例如const,volatile和mutable.
为什么是long long int数据类型?为什么没有命名longLongInt?多字数据类型如何工作?我可以定义自己的吗?
是否可以根据自定义数据类型定义自定义限定符?
例如,考虑3D中的线性代数向量.可以定义一个LinAlVector类,它包含向量的x,y,z分量.
如果我现在需要确保,这个向量是一个单位向量(长度等于1),我想知道我是否可以写
isUnit LinAlVector vec(x,y,z);
Run Code Online (Sandbox Code Playgroud)
其中isUnit是影响行为的修饰符LinAlVector(例如,如果定义了非单位向量,则为编译器错误).
我知道OO直截了当的方法是派生类UnitLinAlVector的LinAlVector.
我知道operator + for std::string不会因为两个而超载const char*.从而,
std::string str = "Hello" + " World";
Run Code Online (Sandbox Code Playgroud)
不起作用; 然而,以下代码完美无缺.
std::string hello = "Hello";
std::string str = hello + " World";
Run Code Online (Sandbox Code Playgroud)
而且,静态字符串连接(没有+运算符)也可以正常工作
std::string str = "Hello" " World";
Run Code Online (Sandbox Code Playgroud)
没有运营商+两个人的技术原因是什么const char*?
这段代码
#include <array>
void foo(const int length) {
std::array<int, length> arr; //line 4
}
int main(){
const int length = 10;
std::array<int,length> arr;
}
Run Code Online (Sandbox Code Playgroud)
失败并显示错误消息
:在函数'void foo(size_t)'中:
:4:27:错误:'length'不是常量表达式
Run Code Online (Sandbox Code Playgroud)std::array<int, length> arr;
https://gcc.godbolt.org/z/MMQXHx
参数length标记为const.
为什么不能将local的const长度std::array作为函数参数传递?
为什么它在main方法中有效?
我知道,像这样的模板方法会起作用
template <size_t length>
void foo() {
std::array<int, length> arr;
}
Run Code Online (Sandbox Code Playgroud) 这段代码给出了有意义的输出
#include <iostream>
int main() {
unsigned int ui = 100;
unsigned int negative_ui = -22u;
std::cout << ui + negative_ui << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
78
Run Code Online (Sandbox Code Playgroud)
变量negative_ui存储-22,但是是一个unsigned int。我的问题是为什么unsigned int negative_ui = -22u;有效。unsigned int商店如何存储负数?是否节省使用或产生不确定的行为?
我使用的是Intel编译器18.0.3。使用该选项时,-Wall不会发生警告。
附言 我已经读过,如果我给一个无符号变量赋一个负值会怎样?以及为什么unsigned int包含负数
由于无法在标头中找到std :: accumulate,因此无法编译此代码段numeric。
#include <algorithm>
#include <vector>
int main () {
std::vector<int> vec{ 1, 2, 3, 4 };
return std::accumulate(vec.begin(), vec.end(),0);
}
Run Code Online (Sandbox Code Playgroud)
该编译器的探险家给了我正确的错误消息
<source>(6): error: namespace "std" has no member "accumulate"
return std::accumulate(vec.begin(), vec.end(),0);
Run Code Online (Sandbox Code Playgroud)
我正在使用RedHat 6和Intel编译器版本18.0.3。如果使用此设置进行编译,则不会出现错误,并且结果很好。即使-Wall使用警告也不会显示。
我的问题是,为什么我没有收到适当的错误消息?
我需要找到带N元素的数组的最小值和最大值.事实是我的程序正在运行,但是当我在网站上提交它时它只给出了我的32分数100,我不知道什么是错的.
#include <iostream>
using namespace std;
int main() {
int N,min,max;
cin >> N;
min = N;
max = N;
int i,x;
for (i = 1; i <= N; ++i) {
cin >> x;
if ( x < min ) {
min = x;
}
if (x > max) {
max = x;
}
}
cout << min <<" "<< max;
return 0;
}
Run Code Online (Sandbox Code Playgroud) cpp中“:”符号的含义是什么?
#include <iostream>
#include <string>
int main () {
std::string word;
std::cin >> word;
int H[1024];
int big_h = 0;
for(char i : word) {
if(H[(int)i-97] > big_h) {
big_h = H[(int)i-97];
}
}
std::cout << big_h * word.size() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 假设我有 vector<string> v
什么 v.back().back()回报?
它是否返回向量的最后一个字符串的最后一个字符串?
它与v.end().end()?
c++ ×9
arrays ×2
algorithm ×1
const ×1
max ×1
min ×1
numeric ×1
overloading ×1
parameters ×1
qualifiers ×1
redhat ×1
string ×1
types ×1
unit-testing ×1
vector ×1