我遇到了以下错误.
Foo.该类的实例存储在std :: vector vec中class B.new和删除该对象来分配内存来创建类A的实例~Foo().代码编译,但我在运行时崩溃了.如果我delete my_a从类的desstructor中禁用Foo.代码运行正常(但会有内存泄漏).
有人可以解释这里出了什么问题并建议修复?
谢谢!
class A{
public:
A(int val);
~A(){};
int val_a;
};
A::A(int val){
val_a = val;
};
class Foo {
public:
Foo();
~Foo();
void createA();
A* my_a;
};
Foo::Foo(){
createA();
};
void Foo::createA(){
my_a = new A(20);
};
Foo::~Foo(){
delete my_a;
};
class B {
public:
vector<Foo> vec;
void createFoo();
B(){};
~B(){};
};
void B::createFoo(){
vec.push_back(Foo());
};
int …Run Code Online (Sandbox Code Playgroud) 以下代码是否正常?:
std::vector<char> var;
size_t requiredSize;
getenv_s(&requiredSize, NULL, 0, "Something");
if (requiredSize == 0)
{
return ENV_NOT_EXIST;
}
if(var.size() < requiredSize)
var.resize(requiredSize);
// Get the value of the environment variable.
getenv_s(&requiredSize, &var[0], requiredSize, "Something");
std::string str(var.begin(),var.end());
Run Code Online (Sandbox Code Playgroud)
如果这段代码没问题,有人可以解释一下如何更新矢量begin()和end()值var吗?看起来这个代码直接改变了向量的内部数组,而不是std::vectorapi - 所以这些值如何更新为实际大小?
为什么第二次填充std :: vector更快?即使从开始预留空间?
int total = 1000000;
struct BaseClass {
float m[16];
int id;
BaseClass(int _id) { id = _id; }
};
int main() {
std::vector<BaseClass> ar;
ar.reserve(total);
{
auto t_start = std::chrono::high_resolution_clock::now();
for (int var = 0; var < total; ++var) {
ar.emplace_back(var);
}
auto t_end = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(
t_end - t_start).count() << "\n";
ar.clear();
}
{
auto t_start = std::chrono::high_resolution_clock::now();
for (int var = 0; var < total; ++var) {
ar.emplace_back(var);
}
auto t_end = std::chrono::high_resolution_clock::now(); …Run Code Online (Sandbox Code Playgroud) 在迭代它时,是否可以在C++ 11中更改向量的大小?很明显,迭代器将失效,但仍然可以使用以下干净的语法?
std::vector<some_type> vec;
for(auto elem : vec) {
if(condition(elem)) {
new_elem = function(elem);
vec.insert(iterator_associated_with_elem+1, new_elem);
}
//Don't insert on condition(new_elem)
}
Run Code Online (Sandbox Code Playgroud)
如果没有,完成此任务的最干净的代码是什么?
为什么以下代码对bool的std :: vector不起作用(如所考虑的)?bool-vector中的一些元素是随机错误的.对于int向量,所有工作正常(使用多于3个循环进行测试).
我在ubuntu 14.04 64bit上使用g ++ 4.8.4.
#include <iostream>
#include <vector>
using namespace std;
class TestBool
{
public:
TestBool() {}
bool test() {return true;}
int testInt() {return 10;}
};
void testBVec(vector<bool> &bv, size_t loop)
{
cout << "boolvec loop " << loop << endl;
for(size_t i = 0; i < bv.size(); i++) {
if( ! bv[i])
cout << "wholy shit with bool at index " << i << endl;
}
}
void testIntVec(vector<int> &iv, size_t loop)
{
cout << …Run Code Online (Sandbox Code Playgroud) 正如标题所示,我试图使用具有SIMD内在数据类型的STL向量.我知道由于加载/存储的潜在开销,这不是一个好习惯,但我遇到了一个非常奇怪的错误.这是代码:
#include "immintrin.h"
#include <vector>
#include <stdio.h>
#define VL 8
int main () {
std::vector<__m256> vec_1(10);
std::vector<__m256> vec_2(10);
float * tmp_1 = new float[VL];
printf("vec_1[0]:\n");
_mm256_storeu_ps(tmp_1, vec_1[0]); // seems to go as expected
for (int i = 0; i < VL; ++i)
printf("%f ", tmp_1[i]);
printf("\n");
delete tmp_1;
float * tmp_2 = new float[VL];
printf("vec_2[0]:\n");
_mm256_storeu_ps(tmp_2, vec_2[0]); // segmentation fault
for (int i = 0; i < VL; ++i)
printf("%f ", tmp_2[i]);
printf("\n");
delete tmp_2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用它编译了它g++ …
我的代码中有一个带有类型签名的重载函数:
void foo(std::string);
void foo(std::vector<std::string>);
Run Code Online (Sandbox Code Playgroud)
我希望foo的用户能够使用字符串或字符串列表来调用它
//Use case 1
foo("str");
//Use case 2
foo({"str1","str2","str3"});
foo({"str1","str2","str3","str4"});
Run Code Online (Sandbox Code Playgroud)
问题是当调用者将两个字符串传递给foo的初始值设定项列表时。
//Problem!
foo({"str1","str2"});
Run Code Online (Sandbox Code Playgroud)
对foo的调用是不明确的,因为它匹配两个类型签名。这是因为显然{"str1","str2"}是有效的构造函数std::string
所以我的问题是,在foo的声明或实现中我可以做些什么,以使我维护上面描述的API而不遇到这种模棱两可的构造方法。
我不想定义自己的字符串类,但是可以定义其他东西,而不必定义其他东西,vector<string>只要它可以使用字符串的初始化列表进行初始化即可。
只是出于好奇,为什么字符串构造函数会接受{"str1","str2"}?
我正在尝试为OpenGL项目创建一个Wavefront(.obj)文件加载器.我正在使用的方法是逐行进行并分离矢量(std :: vectors)中的顶点位置,纹理位置和正常位置,并且我将它们的索引(顶点,纹理和法线索引)存储在三个单独的中向量(来自文件的'f'行,每个面).
我无法根据纹理索引对完整的纹理坐标进行排序.我能够将顶点渲染到正确的位置,因为我的'loader'类需要索引,但我无法弄清楚如何以任何方式对纹理坐标进行排序,因此纹理在某些三角形上看起来像是一个偏移结果.
偏移纹理的多维数据集的图像:

纹理(.png)的图像,它应该如何在每张脸上:

编辑:这是.obj文件和.mtl文件的链接. Google云端硬盘.
这是我的OBJLoader.cpp文件:
rawObj.open(filePath); // Open file
while (!rawObj.eof()) {
getline(rawObj, line); // Read line
// Read values from each line
// starting with a 'v' for
// the vertex positions with
// a custom function (gets the word in a line
// at position i)
if (strWord(line, 1) == "v") {
for (int i = 2; i <= 4; i++) {
std::string temp;
temp = strWord(line, i);
vertexStrings.push_back(temp);
}
// Same for texture …Run Code Online (Sandbox Code Playgroud) 我想使用lambda表达式作为std ::整数集的自定义比较.该网站上有许多解释如何执行此操作的答案,例如/sf/answers/3228982501/.事实上,
#include <vector>
#include <set>
#include <iostream>
int main() {
auto different_cmp = [](int i, int j) -> bool {
return j < i;
};
std::set<int, decltype(different_cmp)> integers(different_cmp);
integers.insert(3);
integers.insert(4);
integers.insert(1);
for (int integer : integers) {
std::cout << integer << " ";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编制和输出
4 3 1
正如所料.但是,当我尝试将此设置放在矢量中时
std::vector<std::set<int, decltype(different_cmp)>> vec_of_integers;
vec_of_integers.push_back(integers);
Run Code Online (Sandbox Code Playgroud)
编译器抱怨.我正在使用Visual Studio 2017,我根据周围的代码得到不同的编译器错误.在上面的例子中,它是
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\utility(77): error C2664: 'void std::swap(std::exception_ptr &,std::exception_ptr &) noexcept': cannot convert argument 1 from '_Ty' …Run Code Online (Sandbox Code Playgroud) 我在向量中插入数据时收到分段错误。我认为载体没有分配。
我不想reserve大小。怎么做?
class A
{
private:
struct data
{
int x;
int y;
};
std::vector<data>Set;
public:
void insert()
{
Set[0].x = 5; Set[0].y = 6;
}
};
A a;
a.insert(); // Segmentation Fault
Run Code Online (Sandbox Code Playgroud)