我有一个,std::vector<std::vector<double>>并希望在它的末尾添加一些元素,所以这是我的试用:
std::vector<std::vector<double> > vec;
vec.emplace_back({0,0});
Run Code Online (Sandbox Code Playgroud)
但这不会编译,而以下将执行:
std::vector<double> vector({0,0});
Run Code Online (Sandbox Code Playgroud)
为什么emplace_back不能在这个位置构造元素?或者我做错了什么?
谢谢你的帮助.
通过尝试,我开始知道有必要在cout语句中将括号放在条件运算符周围.这是一个小例子:
#include <iostream>
int main() {
int a = 5;
float b = (a!=0) ? 42.0f : -42.0f;
// works fine
std::cout << b << std::endl;
// works also fine
std::cout << ( (a != 0) ? 42.0f : -42.0f ) << std::endl;
// does not work fine
std::cout << (a != 0) ? 42.0f : -42.0f;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
42
42
1
Run Code Online (Sandbox Code Playgroud)
为什么这些括号必要?在两种情况下都知道条件运算符的结果类型,不是吗?
我想用以下模式对立体图像文件进行排序
img_i_j.ppm,
Run Code Online (Sandbox Code Playgroud)
其中i是图像计数器,j是摄像机的ID [0,1].目前,如果我使用它们排序
ls -1 *.ppm | sort -n
Run Code Online (Sandbox Code Playgroud)
结果看起来像这样:
img_0_0.ppm
img_0_1.ppm
img_10_0.ppm
img_10_1.ppm
img_1_0.ppm
img_11_0.ppm
img_11_1.ppm
img_1_1.ppm
img_12_0.ppm
Run Code Online (Sandbox Code Playgroud)
但我需要这个输出:
img_0_0.ppm
img_0_1.ppm
img_1_0.ppm
img_1_1.ppm
img_2_0.ppm
img_2_1.ppm
...
img_10_0.ppm
img_10_1.ppm
...
Run Code Online (Sandbox Code Playgroud)
这是否可以在不调整文件名的情况下实现?
我写了一小段代码来确定如何在向量中分配内存.
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<unsigned int> myvector;
unsigned int capacity = myvector.capacity();
for(unsigned int i = 0; i < 100000; ++i) {
myvector.push_back(i);
if(capacity != myvector.capacity())
{
capacity = myvector.capacity();
cout << myvector.capacity() << endl;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在Ubuntu上使用Visual Studio 2008和g ++ 4.5.2编译了这个并获得了以下结果:
视觉工作室:
1 2 3 4 6 9 13 19 28 42 63 94 141 211 316 474 711 1066 1599 2398 3597 5395 8092 12138 18207 27310 …
我有一个关于 QT 中 VAO 的问题。正如这里所假设的,我编写了以下代码用于初始化:
void GLWidget::initializeGL() {
vao.create();
vao.bind();
// setting up buffers
buffer.create();
buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
buffer.bind();
buffer.allocate(vertices.data(), 3 * vertices.size() * sizeof(float));
vao.release();
}
Run Code Online (Sandbox Code Playgroud)
在绘画阶段,我只需执行以下操作:
void GLWidget::paintGL() {
// some other code
vao.bind();
if(shader_program->isLinked()) {
shader_program->setUniformValue("mvpMatrix", p_matrix * v_matrix * m_matrix);
// the interesting part about the buffer:
shader_program->setAttributeBuffer("vertex", GL_FLOAT, 0, 3);
shader_program->enableAttributeArray("vertex");
// the drawing
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
shader_program->disableAttributeArray("vertex");
}
vao.release();
}
Run Code Online (Sandbox Code Playgroud)
就我只处理一个 VAO 而言,这是有效的。但是,当使用多个 VAO 时,不会绘制任何内容或仅绘制一个“对象”(缓冲区)。每个对象的代码完全相同,因此绘图始终是
所以问题是如何使用包括不同缓冲区的多个 vao。
我写了以下MISRA不喜欢的代码:
UartPtr->C &= ((uint8_t)(~SIO_C2_SBK));
Run Code Online (Sandbox Code Playgroud)
同
#define SIO_C2_SBK ((uint8_t)0x01u)
Run Code Online (Sandbox Code Playgroud)
并被UartPtr定义为
UartPtr = (UartStruct*) 0x12345678; /* I know that this is also a violation of MISRA */
Run Code Online (Sandbox Code Playgroud)
与基础数据结构:
typedef volatile struct UartStructTag
{
uint8_t BDH;
uint8_t BDL;
uint8_t C1;
uint8_t C2;
} UartStruct;
Run Code Online (Sandbox Code Playgroud)
我的Misra检查员抱怨第一行,并说明
具有负值的整数常量表达式将转换为无符号类型.
但是,以下行不会导致MISRA出现问题:
UartPtr->C |= ((uint8_t)(SIO_C2_SBK));
Run Code Online (Sandbox Code Playgroud)
所以问题来自于逐位否定.但由于所有操作都直接转换为uint8_t,因此我不会违反MISRA标准.谁想帮助我呢?
在工作期间,我看到了以下代码片段,现在我想知道,如果有理由将"case-values"转换为更大的数据类型.我猜,它用于提供以后有超过256个不同状态的可能性,但是,状态变量也必须更大.这是我正在谈论的代码:
#define STATE_1 (uint8_t)(0x00)
#define STATE_2 (uint8_t)(0x01)
...
#define STATE_n (uint8_t)(0x..)
void HandleState(uint8_t state)
{
switch(state)
{
case (uint16_t)STATE_1:
// handle state 1
break;
case (uint16_t)STATE_2:
// handle state 2
break;
...
case (uint16_t)STATE_n:
// handle state n
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
还有其他原因吗?