我需要在写入/发送数据到某个文件(数据库,Excel等)时进行转换std::vector并_variant_t避免循环
你能告诉我怎么办吗?
我试图得到一个std::vector<_variant_t>并以某种方式放入另一个_variant_t变量,但不支持直接转换,即使它已经完成,也没有任何方法或某种包装器将此向量转换为_variant_t变量.
我宁愿不处理任何循环.
我很惊讶在调用erase时发现了vector :: erase移动元素.我认为它会将最后一个元素与"待删除"元素交换,并将大小减小一个.我的第一反应是:"让我们扩展std :: vector并覆盖erase()".但是我在很多线程中发现了" 从C++ STL容器中获取真的风险吗? ",它会导致内存泄漏.但是,我没有向vector添加任何新的数据成员.因此没有额外的内存可以释放.还有风险吗?
有些人认为我们应该更喜欢构成而不是继承.在这种情况下,我无法理解这个建议.为什么我要浪费我的时间在"机械"任务中包装其他精彩的std :: vector类的每个函数.继承确实对这项任务最有意义 - 或者我错过了什么?
我有一长串数据(n个实体).此数组中的每个对象都有一些值(比方说,对象的m值).我有一个周期:
myType* A;
// reading the array of objects
std::vector<anotherType> targetArray;
int i, j, k = 0;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
{
if (check((A[i].fields[j]))
{
// creating and adding the object to targetArray
targetArray[k] = someGenerator(A[i].fields[j]);
k++;
}
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我有n*m个有效对象,有些(n*m)/ 10或更少.
问题是如何为内存分配内存targetArray?
targetArray.reserve(n*m);
// Do work
targetArray.shrink_to_fit();
计算元素而不生成对象,然后根据需要分配尽可能多的内存,再循环一次.
在每次创建新对象的迭代中调整数组大小.
我在每个方法中都看到了一个巨大的战术错误.是另一种方式吗?
给定一个十六进制std::string像"09e1c5f70a65ac519458e7e53f36",我怎么能在两位数块分割它,并将其存储到std::vector<uint8_t>?
我按照块大小的步骤循环遍历字符串,但我不知道如何将十六进制块转换为数字.这就是我到目前为止所拥有的.
vector<byte> vectorify(string input, int chunk = 2)
{
vector<uint8_t> result;
for(size_t i = 0; i < input.length(); i += chunk)
{
int hex = input.substr(i, chunk);
// ...
}
return result;
}
Run Code Online (Sandbox Code Playgroud) 我目前正在使用pugi xml,我经常使用这样的循环:
for (pugi::xml_node sth: root.child("name").children())
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
然后在某些时候我意识到我需要在WHICH迭代中保存信息,我找到了一些值,因为稍后需要在此循环之外.我可以在不添加计数器的情况下判断我正在进行哪次迭代吗?
此外,如果该对象是这样的矢量:
std::vector<type> vtr;
for (std::vector<type>::iterator it = vtr.begin(); it != vtr.end(); ++it)
{
//which iteration?
}
Run Code Online (Sandbox Code Playgroud) 我试图在向量的一部分中找到从下一个迭代器位置到向量末尾的边界.
代码是:
#include <algorithm>
#include <vector>
#include <iostream>
int
main()
{
typedef std::vector<int> Vector;
Vector v;
v.push_back(3);
v.push_back(7);
v.push_back(15);
v.push_back(21);
std::sort(v.begin(), v.end());
for (Vector::const_iterator i = v.begin(); i != v.end(); ++i) {
Vector::const_iterator low = std::lower_bound(i+1, v.end(), -10-*i);
Vector::const_iterator high = std::upper_bound(i+1, v.end(), 10-*i);
for (Vector::const_iterator j = low; j != high; ++j) {
std::cout << *i << "~" << *j << std::endl;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,std::lower_bound(i+1, v.end(), -10-*i)触发编译错误,我无法理解:
c++ -O3 -ggdb -std=c++11 -W -Wall -pedantic …Run Code Online (Sandbox Code Playgroud) 是否有任何特殊原因std::vector没有会员功能find?相反,你必须打电话给std::find(和#include <algorithm>).
我之所以要问的是,我认为能够在某些实现中更改容器类是一件好事,而无需在访问容器的任何地方更改代码.假设我将std::vector实现使用的位置替换std::find为std::map.然后我还必须std::find用调用成员替换调用find,除非我想保持线性复杂性std::find.为什么不只是find为所有容器类都有一个成员,它找到一个最适合每个容器的算法的元素?
我正在研究一种算法的实现,我想表明它可以在恒定时间内工作,即使有很多元素也是如此.
不幸的是,我需要一个存储元素的数据结构.当元素的数量非常高,但对我的算法而言并非不合理时,std :: vector和std :: valarray都不会在常量时间访问任意元素,如此图所示.
是否有更好的数据结构来存储值?是否有任何技术可以实现以实现恒定时间访问?
当std :: vector重新分配其内存数组时,使用什么样的复制/移动构造函数将元素复制/移动到新房子?
I have a program that calculates some values in different threads with std::packaged_task<int()>. I store the std::future I get from the packaged tasks via get_future() in a vector (defined as std::vector<std::future<int>>).
When I calculate the sum of all the tasks I use a for loop and it's working:
// set up of the tasks
std::vector<std::future<int>> results;
// store the futures in results
// each task execute in its own thread
int sum{ 0 };
for (auto i = …Run Code Online (Sandbox Code Playgroud) c++ ×10
stdvector ×10
c++11 ×3
std ×2
stl ×2
com ×1
composition ×1
hex ×1
inheritance ×1
memory-leaks ×1
pugixml ×1
std-future ×1
valarray ×1
variant ×1