我有下一个代码片段.我的想法是,vector有5个项目,我通过operator []访问100,这应该会导致崩溃.但正如你在输出中看到的那样,它有效.
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec(5, 1);
vec[100] = 25;
std::cout << "vec[100] = " << vec[100] << ", vec[99] = " << vec[99] <<
", vector size = " << vec.size() <<
", vector capacity = " << vec.capacity() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
vec[100] = 25, vec[99] = 0, vector size = 5, vector capacity = 5
Run Code Online (Sandbox Code Playgroud)
编译标志:
clang++ -W -Wall -std=c++14 -stdlib=libc++ vector_over_flow_test.cpp -o vector_overflow_test.bin
Run Code Online (Sandbox Code Playgroud)
铿锵版:
$clang++ --version
Apple LLVM version 8.0.0 (clang-800.0.42.1) …Run Code Online (Sandbox Code Playgroud) 如果我写:
#include <map>
int main()
{
std::map<int, double> q;
q[3] += 4;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我能确定q [3]是4,而不是q [3]是4 +(来自内存的一些随机未初始化的垃圾)?
我有一个自定义的异常类,派生自std::exception:
CameraControlException.h:
#ifndef CAMERACONTROLEXCEPTION_H
#define CAMERACONTROLEXCEPTION_H
#include <QString>
#include <exception>
#include "ProxOnyx/ProxOnyxUsb1_3M.h"
class CameraControlException : public std::exception
{
public:
explicit CameraControlException(QString statusText, int errorNumber);
virtual const char *what() const noexcept;
private:
QString errorMessage;
int errorNumber;
};
#endif // CAMERACONTROLEXCEPTION_H
Run Code Online (Sandbox Code Playgroud)
CameraControlException.cpp:
#include "CameraControlException.h"
#include <iostream>
CameraControlException::CameraControlException(QString statusText, int errorNumber) :
errorMessage(QString("Status: ").append(statusText)),
errorNumber(errorNumber)
{
this->errorMessage.append("\nSome text in new line");
}
const char *CameraControlException::what() const noexcept {
// Output the message like return them:
std::cout << "this->errorMessage.toLatin1().data(): " …Run Code Online (Sandbox Code Playgroud) 在std::thread:
为什么?这种API有什么用?
它是线程概念的基础.
堆栈大小,为什么我们不关心内存?也许作者只假设Linux和Windows具有分页内存和64位地址空间,但是没有分页内存的平台呢?
优先事项,如何使任何系统具有可预测的时间而没有优先级?
https://en.cppreference.com/w/cpp/thread/lock_tag
void transfer(bank_account &from, bank_account &to, int amount)
{
// lock both mutexes without deadlock
std::lock(from.m, to.m);
// make sure both already-locked mutexes are unlocked at the end of scope
std::lock_guard<std::mutex> lock1(from.m, std::adopt_lock);
std::lock_guard<std::mutex> lock2(to.m, std::adopt_lock);
// equivalent approach:
// std::unique_lock<std::mutex> lock1(from.m, std::defer_lock);
// std::unique_lock<std::mutex> lock2(to.m, std::defer_lock);
// std::lock(lock1, lock2);
from.balance -= amount;
to.balance += amount;
}
Run Code Online (Sandbox Code Playgroud)
通过一次锁定两个互斥锁可以获得什么?
他们在这里得到了什么?
请解释他们的决定背后的原因.
我试图重载运算符+和+ = std :: vector,我做的是
namespace std {
template<class T>
vector<T> operator+(vector<T> x, vector<T> y) {
vector<T> result;
result.reserve(x.size());
for (size_t i = 0; i < x.size(); i++)
result[i] = x[i] + y[i];
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
但我认为这是不好的做法,因为clang-tidy警告我"修改std命名空间会导致未定义的行为".在为STL类重载运算符时还有其他更好的做法吗?
我有一个二维的std::array,
std::array<std::array<string, n_height>, n_width> data_able;
Run Code Online (Sandbox Code Playgroud)
n_height并且n_width是常量变量,我不知道它们的值是否不同dataTables,并且获取它们的值的唯一可能方法是使用函数调用:
const size_t n_height = dcmI_image->get_height();
const size_t n_width = dcm_image->get_width();
Run Code Online (Sandbox Code Playgroud)
但这是不可能的,这就是我得到的一个错误:
error: the value of ‘n_height’ is not usable in a constant expression
‘n_height’ was not initialized with a constant expression
Run Code Online (Sandbox Code Playgroud)
当然,对于nWidth也是如此。
我今天偶然发现了一件非常微妙的事情.这里有一些代码来演示:
#include <set>
#include <iostream>
class MyClass {
private:
int id = -1;
public:
MyClass() {}
MyClass(int _id) : id(_id) {}
bool operator() (const MyClass& instance1, const MyClass& instance2) const {
std::cout << id << std::endl;
std::cout << instance1.id << std::endl;
std::cout << instance2.id << std::endl;
return true;
}
};
int main(int argc, char *argv[])
{
std::set<MyClass, MyClass> classSet;
classSet.insert(MyClass(1));
classSet.insert(MyClass(2));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
-1
2
1
Run Code Online (Sandbox Code Playgroud)
当我使用设置id值的专用构造函数创建所有类实例时,我很惊讶this->id评估为-1.显然,C++使用标准构造函数创建一些虚拟实例.
我的问题是:
this->id是1或2)?或者我是否总是必须在内部使用标准构造函数为C++准备我的类?我正在尝试将多个键值对添加到具有struct作为其键和值的映射中.由于某些原因,只有我添加的第一个键值对被添加到地图中,并且找不到第二个键值对.
#include<string>
#include<iostream>
#include<map>
using namespace std;
struct RJNodeAddress
{
string m_ip;
string m_port;
bool operator<(const RJNodeAddress &l)const
{
int l_size=l.m_ip.size();
int r_size=l.m_port.size();
return (l_size < r_size);
}
};
struct RJNodeDetails
{
string m_NodeType;
int m_appId;
};
class RJWebsocket
{
public:
static map<RJNodeAddress,RJNodeDetails> m_Nodes;
};
map<RJNodeAddress,RJNodeDetails> RJWebsocket::m_Nodes;
int main()
{
RJNodeAddress l_node1,l_node2;
RJNodeDetails l_add1,l_add2;
l_node1.m_ip="172.16.129.68";
l_node1.m_port="8766";
l_node2.m_ip="172.16.128.130";
l_node2.m_port="8768";
l_add1.m_appId=0;
l_add1.m_NodeType="CM";
l_add1.m_appId=1;
l_add1.m_NodeType="CM";
RJWebsocket::m_Nodes.insert({l_node1,l_add1});
RJWebsocket::m_Nodes.insert({l_node2,l_add2});
for(const auto& j:RJWebsocket::m_Nodes)
{
cout<<endl<<"Iterating through RJWebsocket::m_Nodes"<<endl;
cout<<endl<<"port: "<<j.first.m_port<<" "<<"IP: "<<j.first.m_ip<<endl;
} …Run Code Online (Sandbox Code Playgroud) 我正在使用函数指针与std :: function, 并遇到了以下问题.
让我们考虑下面的代码:
#include <cmath>
#include <functional>
// g++ -std=c++17 SF.C -o SF
// clang++ -std=c++17 SF.C -o SF
int main()
{
typedef double (*TpFunctionPointer)(double) ;
TpFunctionPointer pf1 = sin; // o.k.
TpFunctionPointer pf2 = std::sin; // o.k
TpFunctionPointer pf3 = std::riemann_zeta; // o.k
std::function< double(double) > sf1( sin ); // o.k
std::function< double(double) > sf2( std::sin ); // fails
std::function< double(double) > sf3( std::riemann_zeta ); // fails
}
Run Code Online (Sandbox Code Playgroud)
用函数指针pf1,pf2,pf3和sf1 编译g++ v8.2或clang …