我注意到 C 和 C++ 的循环范围规则for
不同。
例如,下面的代码在 C 编译器中合法,但在 C++ 编译器中不合法。
for (int i = 0; i < 10; ++i) {
int i = 5;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在 C 中有效,但在 C++ 中给出了重新定义错误。
我的猜测是,C 编译器将循环视为循环内有另一个作用域,如下所示。
for (int i = 0; i < 10; ++i) {
{
int i = 5;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么 C 编译器允许在循环范围内定义第二个同名变量?这样做有什么特别的原因或优势吗?
#include <compare>
#include <iostream>
int main()
{
auto comp1 = 1.1 <=> 2.2;
auto comp2 = -1 <=> 1;
std::cout << typeid(comp1).name()<<"\n"<<typeid(comp2).name();
}
Run Code Online (Sandbox Code Playgroud)
输出:
结构 std::partial_ordering
结构 std::strong_ordering
我知道如果操作数具有整数类型,则运算符将返回一个 prvalue 类型std::strong_ordering
。我也知道操作数是否有浮点类型,运算符会产生类型的纯右值std::partial_ordering
。
但是为什么我应该使用三向比较运算符而不是双向运算符(==
, !=
, <
, <=
, >
, >=
)?这对我有什么好处吗?
鉴于此类层次结构:
#include <iostream>
class Base {
public:
Base() = default;
Base(const Base&) { std::cout << " copy\n"; }
template<typename T>
Base(T&&) { std::cout << " T&&\n"; }
};
class Sub : public Base {
public:
using Base::Base;
};
Run Code Online (Sandbox Code Playgroud)
这是已知的,该代码将打印T&&
:
// objects
Base varObj;
const Base constObj;
// invoking constructors
Base copy(varObj); // T&&
Base move(std::move(constObj)); // T&&
Base number(42); // T&&
Run Code Online (Sandbox Code Playgroud)
为什么使用Sub
而不是Base
“修复”问题?
// objects
Sub varObj;
const Sub constObj;
// invoking constructors …
Run Code Online (Sandbox Code Playgroud) 当头文件中的这个类被添加到语言中时,我们将能够更轻松地处理哪些问题以及计划替换哪些语法?下面我分享了从cppreference网站获得的代码。
班级std::stacktrace_entry
namespace std {
class stacktrace_entry {
public:
using native_handle_type = /* implementation-defined */;
// constructors
constexpr stacktrace_entry() noexcept;
constexpr stacktrace_entry(const stacktrace_entry& other) noexcept;
constexpr stacktrace_entry& operator=(const stacktrace_entry& other) noexcept;
~stacktrace_entry();
// observers
constexpr native_handle_type native_handle() const noexcept;
constexpr explicit operator bool() const noexcept;
// query
string description() const;
string source_file() const;
uint_least32_t source_line() const;
// comparison
friend constexpr bool operator==(const stacktrace_entry& x,
const stacktrace_entry& y) noexcept;
friend constexpr strong_ordering operator<=>(const stacktrace_entry& x,
const stacktrace_entry& y) …
Run Code Online (Sandbox Code Playgroud) 假设我有以下代码:
void some_function(std::string_view view) {
std::cout << view << '\n';
}
int main() {
some_function(std::string{"hello, world"}); // ???
}
Run Code Online (Sandbox Code Playgroud)
view
inside会some_function
指的string
是已被破坏的吗?我很困惑,因为考虑到这段代码:
std::string_view view(std::string{"hello, world"});
Run Code Online (Sandbox Code Playgroud)
产生警告(来自clang++
):
warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling-gsl]
Run Code Online (Sandbox Code Playgroud)
有什么不同?
(奇怪的是,使用大括号{}
而不是方括号()
来初始化string_view
上面的内容消除了警告。我也不知道为什么会这样。)
需要明确的是,我理解上面的警告( 的string_view
寿命比 长string
,所以它持有一个悬空指针)。我要问的是为什么传递 astring
不会some_function
产生相同的警告。
这是发生冲突的一小段代码。有什么方法可以正确解决这个问题吗?
#define DEBUG 0
enum class TypeEnum : int
{
DEBUG = 0,
INFO = 1
};
Run Code Online (Sandbox Code Playgroud) 假设我有这个向量向量[[5,10],[2,5],[4,7],[3,9]]
,我想使用sort()
cpp的方法对它进行排序,这样[[5,10],[3,9],[4,7],[2,5]]
排序后就变成这样了。那就是我想根据第二个索引进行排序。
现在我已经编写了这段代码来对向量的向量进行排序,但它无法正常工作。
static bool compareInterval( vector<vector<int>> &v1, vector<vector<int>> &v2)
{
return (v1[0][1]>v2[0][1]);
}
sort(boxTypes.begin(), boxTypes.end(), compareInterval);
Run Code Online (Sandbox Code Playgroud)
谁能告诉我哪里出了问题,我该如何纠正。提前致谢。
我试图将类型作为参数传递给一个方法,该方法将正确构造对象并将其推送到 unique_ptr 向量,但是创建的对象始终是 Base 对象。使用 emplace_back() 时会发生这种情况,如果我只是实例化对象,同样可以正常工作。
在向量外部构造对象效果很好,但是我不确定如何将指针移动到向量。
body_parts.hpp
#include <vector>
#include <string>
#include <fmt/core.h>
using namespace std;
namespace uhcr {
class body_part
{
public:
string name = "Generic";
template <class T>
void add_body_part()
{
this->body_parts.emplace_back<T*>(new T());
fmt::print("{}\n", this->body_parts.back()->name);
}
private:
vector<unique_ptr<body_part>> body_parts;
};
class torso : public body_part
{
public:
string name = "Torso";
};
}
Run Code Online (Sandbox Code Playgroud)
字符.hpp
#include <string>
#include "components/body_parts.hpp"
using namespace std;
namespace uhcr {
class character : public body_part
{
public:
string name = "Character";
}; …
Run Code Online (Sandbox Code Playgroud) 我试图读取这样的输入:
5
1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
第一个是数组的大小,另一个是数组,输出只是读取的数组。
但是使用这段代码,我不断得到这个输出:
1 2 3 4 5 0 1871824307 62958 7346864 0 7340368 0
Run Code Online (Sandbox Code Playgroud)
我真的想不通为什么。
#include<iostream>
using namespace std;
int* readArray()
{
int n;
cin>>n;
int *arr = new int[n];
for (int i=0;i<n;i++){
cin>>arr[i];
}
return arr;
}
int main()
{
int *p= readArray();
int *q = p;
while(*p !=-1)
{
cout<<*p<<" ";
p = p + 1;
}
delete[] q;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我是 C++ 新手,我想excel
使用 C++ 将值输入电子表格,我知道我们可以使用fstream
但如何使用此方法获取特定列或行来处理文件。
我试图了解 std::move 。在我的代码中,我正在从内部包含两个字段的std::list<struct Data>
位置移动一个元素,但我没有得到预期的输出。这是我的代码:struct Data
std::string
#include <iostream>
#include <string>
#include <list>
struct Data {
std::string topic {};
std::string msg {};
Data(const std::string& topic, const std::string& msg) {
this->topic = topic;
this->msg = msg;
}
};
int main() {
std::list<Data> data_list;
data_list.push_back(Data("A", std::string(1000, 'a')));
data_list.push_back(Data("B", std::string(1000, 'b')));
data_list.push_back(Data("C", std::string(1000, 'c')));
data_list.push_back(Data("D", std::string(1000, 'd')));
data_list.push_back(Data("E", std::string(1000, 'e')));
while (!data_list.empty()) {
std::cout << (void*)&data_list.front() << "\n";
Data&& d1 = std::move(data_list.front());
data_list.pop_front();
std::cout << d1.topic << ", " << d1.msg …
Run Code Online (Sandbox Code Playgroud) 有两个值:
QString str1 = "3.5.8", str2 = "20.3.6";
Run Code Online (Sandbox Code Playgroud)
让我们想象一下,这两个numbers
代表了软件version
。
众所周知,QString
逐个字符进行比较。
如果我们以这种方式做出这个决定会怎么样:
str1.replace(".","");
str2.replace(".","");
int n = str1.toInt();
int m = str2.toInt();
if (n >= m)
{
qDebug() << "YES";
} else if (n <= m) {
qDebug() << "NO";
}
Run Code Online (Sandbox Code Playgroud)
也许有一个更优化和正确的方法来做到这一点。
您能否告诉我如何将这些值转换为数字,以便可以对它们进行整体比较。谢谢。
以下代码编译并正常工作:
#include<iostream>
class Base {
protected:
int _a;
public:
virtual ~Base()=default;
Base(int a) : _a{a} {};
int getit() const { return _a; }
};
class Derived : public Base {
public:
Derived(int a) : Base{a} {};
int get2() const { return 2*this->_a; }
};
int main() {
Base D{2};
auto* ptr = &D;
auto* ptr2 = static_cast<Derived*>(ptr);
std::cout << ptr2->get2() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出
4
Run Code Online (Sandbox Code Playgroud)
如果我改变static_cast
了dynamic_cast
它出现segfaults。
我的问题:
使用static_cast
强制转换为不添加任何数据成员的派生类是否安全?
c++ ×13
c++17 ×2
stdvector ×2
arrays ×1
c ×1
c++20 ×1
c++23 ×1
dynamic-cast ×1
emplace ×1
enum-class ×1
excel ×1
for-loop ×1
qstring ×1
qt ×1
scope ×1
sorting ×1
static-cast ×1
stdlist ×1
stdmove ×1
stdstring ×1
string-view ×1
templates ×1
unique-ptr ×1