如果有可能将std :: vector <>强制转换为双指针,我想知道是出于好奇.
我从来没有遇到过以这种方式将std :: vector作为指针传递的问题:
std::vector<char> myCharVector;
myCharVector.push_back('a');
myCharVector.push_back('b');
myCharVector.push_back('c');
char *myCharPointer = &myCharVector[0];
Run Code Online (Sandbox Code Playgroud)
所以我很好奇是否有可能以类似的方式分配指针的地址:
char *myPointer = "abc";
char **myDoublePointer = &myPointer;
Run Code Online (Sandbox Code Playgroud)
我试过了:
char **myDoublePointer = (char**)&myCharVector;
Run Code Online (Sandbox Code Playgroud)
但它不起作用.有没有办法实现这个目标?
首先,如果这是一个令人眼花缭乱的简单而明显的问题,我想道歉.我知道对于拥有专业知识的人来说,这是一件相当容易的事情.C++ 11允许以列表形式初始化向量:
std::vector<std::string> v = {
"this is a",
"list of strings",
"which are going",
"to be stored",
"in a vector"};
Run Code Online (Sandbox Code Playgroud)
但这在旧版本中不可用.我一直在努力想出填充字符串向量的最佳方法,到目前为止我唯一能想到的就是:
std::string s1("this is a");
std::string s2("list of strings");
std::string s3("which are going");
std::string s4("to be stored");
std::string s5("in a vector");
std::vector<std::string> v;
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
v.push_back(s4);
v.push_back(s5);
Run Code Online (Sandbox Code Playgroud)
它有效,但写作有点苦差事,我相信有更好的方法.
我有示例类:
class Example {
private:
int testValue1;
int testValue2;
int testValue3;
public:
Example(int pVal1, int pVal2, int pVal3);
Example(const Example);
const Example operator =(const Example);
inline int getValue1() { return testValue1; }
inline int getValue2() { return testValue2; }
inline int getValue3() { return testValue3; }
};
Run Code Online (Sandbox Code Playgroud)
在源代码中,我有示例对象的std :: vector.
是否有可能使用某些std :: algorithm,std :: numeric函数将向量中所有Obejcts的Value1求和
像这样的东西:std :: accumulate(vector.begin(),vector.end(),0,SomeFunctorOrOthers)....
当然我可以使用迭代器......但如果有可能,我想知道它
非常感谢你!
我有初始化以下向量的问题:
int main()
{
...
int size = classData.size();
vector<vector<string>> arrayClass[size][3]; // <-- problem
for(int i = 0 ; i < classData.size(); i++)
{
for(int j = 0 ; j < 3; j++)
{
arrayClass[i][j] = classData[j+i];
}
}
}
Run Code Online (Sandbox Code Playgroud)
它说size必须是恒定的价值.有什么想法吗?
在下面的代码中,目的是foo组合调用每个元素vector<Grid> gr.是否有内置的STL功能,如果没有,对大型容器执行此操作的最佳方法是什么?注意,既然grid[0]影响grid[1]也是如此,grid[1]不应该调用该函数grid[0].所以,没有排列,只有组合.顺便说一句,这篇文章没有回答我的问题.
#include <iostream>
#include <vector>
using namespace std;
struct Grid
{
void foo (Grid& g) {}
};
int main()
{
vector<Grid> gr(3);
gr[0].foo (gr[1]);
gr[0].foo (gr[2]);
gr[1].foo (gr[2]);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我必须从c ++中读取一些.mat数据文件,我通过文档阅读,但我想知道如何以干净和优雅的方式处理数据,例如使用std:vector(modest .mat文件大小(10M~) 1G),但应该认真对待记忆问题)
我的功能是这样的:
#include <stdio.h>
#include "mat.h"
#include <vector>
int matread(const char *file, const vector<double>& pdata_v) {
MATFile *pmat;
pmat=matOpen("data.mat","r");
if (pmat == NULL) {
printf("Error opening file %s\n", file);
return(1);
}
mxArray *pdata = matGetVariable(pmat, "LocalDouble");
// pdata -> pdata_v
mxDestroy pa1; // clean up
return 0;
}
Run Code Online (Sandbox Code Playgroud)
那么,问题是,如何有效和安全地从mxArray*pdata数组复制到矢量pdata_v?
我目前正在使用OpenGL,创建一个"纹理缓存"来处理加载图像并使用OpenGL缓冲它们.如果无法加载图像文件,则需要回退到我在构造函数中硬编码的默认纹理.
我基本上需要做的是创建一个统一颜色的纹理.这不是太难,它只是一个像素*颜色通道的数组.
我正在使用std :: vector来保存初始数据,然后再上传OpenGL.我遇到的问题是我无法找到有关使用重复模式初始化矢量的最佳方法的任何信息.
我遇到的第一种方法是使用循环.
std::vector<unsigned char> blue_texture;
for (int iii = 0; iii < width * height; iii++)
{
blue_texture.push_back(0);
blue_texture.push_back(0);
blue_texture.push_back(255);
blue_texture.push_back(255);
}
Run Code Online (Sandbox Code Playgroud)
然而,这似乎是低效的,因为向量必须多次调整自身大小.即使我先保留空间并执行循环,它仍然没有效率,因为内容将在循环之前归零,这意味着每个unsigned char的两次写入.
目前我正在使用以下方法:
struct colour {unsigned char r; unsigned char g; unsigned char b; unsigned char a;};
colour blue = {0, 0, 255, 255};
std::vector<colour> texture((width * height), blue);
Run Code Online (Sandbox Code Playgroud)
然后我使用以下方法提取数据:
reinterpret_cast<unsigned char*>(texture.data());
Run Code Online (Sandbox Code Playgroud)
有比这更好的方法吗?我是C/C++的新手,我会说实话,指针会吓到我.
我创建了一个简单的C++程序来测试C++向量中erase()的行为.
这是我的代码:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// your code goes here
vector<int> vec;
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
cout << vec[0] << " " << vec[1] << " " << vec[2] << endl;
vec.erase(vec.end());
//vec.erase(vec.begin());
cout << vec.size() << endl;
vec.push_back(12);
vec.push_back(10);
cout << vec[0] << " " << vec[1] << " " << vec[2] << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是上面的代码在尝试擦除向量中的最后一个元素时给出了分段错误.但是当我使用begin()擦除第一个元素时,它工作正常.我无法理解它背后的原因.任何帮助将受到高度赞赏.
我可以做的一件事是分配一个大小为n的向量并存储所有数据,然后使用sort(begin(),end())对其进行排序.另外,我可以继续将数据放在一个地图中或设置自己订购的数据,这样我就不用事了.但在这种情况下,由于重新排列(我猜),插入元素可能会更昂贵.
因此,对于大范围的n(对象的数量)来说,这是最短时间的最佳选择
通过函数时,我遇到了常量单元素std :: vector的问题.当std :: vector变量包含单个元素时,C++编译器会自动调用错误的函数.这是C++设计的政策.但是,在这种情况下是否有任何明确的方法来指定.以下是问题的例子
assume i have two overload functions both have the same name "foo"
void foo(const std::vector<int> A)
{
// do vector operator
printf("vector thing");
}
void foo(int a)
{
// do integer operator
printf("integer thing")
}
Run Code Online (Sandbox Code Playgroud)
通常情况下,这两个函数都被正确调用
foo({1,2,3}); // print do vector thing
foo( 3 ); // print do integer thing
Run Code Online (Sandbox Code Playgroud)
但是来自c ++规则.打电话的时候
foo({5}); // print do integer thing ( I want it to call as vector )
Run Code Online (Sandbox Code Playgroud)
其中一种方法是创建一个变量
std::vector<int> B = { 5 };
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题. …