我在全局范围内有一个myObjects的向量.
std::vector<myObject>
Run Code Online (Sandbox Code Playgroud)
方法传递指向向量中的一个元素的指针.这个方法可以递增指针,到达下一个元素,
myObject* pmObj;
++pmObj; // the next element ??
Run Code Online (Sandbox Code Playgroud)
或者它应该通过std::Vector<myObject>::iterator和增加而不是?
现在假设在此期间矢量不会改变.
我正在测试我在计算机上制作一维矢量的大小.为此,我使用以下MWE:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<double> vec;
const unsigned long long lim = 1E8;
for(unsigned long long i=0; i<lim; i++)
{
vec.push_back(i);
}
cout << vec.max_size() << endl; //outputs 536.870.911 on my 32-bit system
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如图所示,max_size()我std::vector可以在系统中包含536.870.911个元素.但是,当我运行上面的MWE时,我得到了错误
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
我的电脑有2GB内存,但1E8整数只占381MB,所以我不明白为什么会出现bad_alloc错误?
我试图从迭代器对创建一个stl向量,我不知道向量可能有多少元素.它可能只有一个元素.
#include <iostream>
#include <vector>
int main()
{
using namespace std;
vector<int> vect;
for (int nCount=0; nCount < 6; nCount++)
vect.push_back(nCount);
vector<int> second(vect.begin(), vect.begin() );
vector<int>::iterator it; // declare an read-only iterator
it = second.begin(); // assign it to the start of the vector
while (it != second.end()) // while it hasn't reach the end
{
cout << *it << " "; // print the value of the element it points to
it++; // and iterate to the next element …Run Code Online (Sandbox Code Playgroud) 刚刚开始学习矢量,但我从VC++ 2010得到了这个错误并且卡在那里,请帮助....
"错误C2228:'.push_back'的左边必须有class/struct/union"
#include <vector>
#include <iostream>
#include <limits>
using namespace std;
int main()
{
vector<double> myVector();
double temp = 30.0;
myVector.push_back(temp); //this line makes error ?? why??
return 0;
}
Run Code Online (Sandbox Code Playgroud) 获取第一个元素的地址std::vector并像原始数组一样使用它是否安全?数据是否连续排列?什么c ++标准说(c++98vs c++11)?这是一种常见的做法吗?
我有一些大小可能变化的数组和函数,它将指针和元素数量作为参数.管理数据并std::vector同时使用它会很棒my_func.
std::vector<int> my_vector;
void my_func(int* int_ptr, int num_ints);
// ..
my_func(&my_vector[0], my_vector.size());
Run Code Online (Sandbox Code Playgroud) 我正在学习C++,我遇到了这个函数的问题.我最初传递了整个向量,然后传递了一个指向向量的指针; 但我似乎无法从指向矢量的指针赋值.
void fill(std::vector<int> *vec, const int startVal)
{
const int n = vec->size();
for (int i = 0; i < n; ++i)
{
vec[i] = startVal + i
}
}
Run Code Online (Sandbox Code Playgroud)
如何在指针(vec)的第i个位置为向量分配值?
我试过了:
vec->[i] = startVal + i;
Run Code Online (Sandbox Code Playgroud) 我为什么要使用a std::stack或a std::queue而不是a std::vector或a std::deque?
由于容器适配器只是标准容器周围的包装器,为什么要使用它们呢?
这是代码:
vector<double> samples;
int main()
{
samples.resize(100);
for(int i=0; i<100; i++) {
samples[i]=i/100.0;
}
samples.clear();
cout << "vector size: " << samples.size() << endl;
cout << "... but samples[9]=" << samples[9] << endl;
}
Run Code Online (Sandbox Code Playgroud)
并输出它:
vector size: 0
... but samples[9]=0.09
Run Code Online (Sandbox Code Playgroud)
清除向量(大小为0)后,我仍然可以访问其数据.这是正常的吗?
参考说元素将被"销毁",但它似乎并不意味着"默认/空"值.
在其他语言中,我会在运行时收到"超出范围"的错误消息...
我想知道下面的代码是否合法.
基本上我有一个std::vector<int>,我有一个处理ints 数组的遗留函数.由于a的元素std::vector总是连续的,所以代码应该始终有效(它实际上适用于我的实现),但它对我来说似乎仍然是一个黑客.
#include <vector>
#include <iostream>
void LecagyFunction(int *data, int length)
{
for (int i = 0; i < length; i++)
std::cout << data[i] << std::endl;
}
int main()
{
std::vector<int> vector;
vector.push_back(5);
vector.push_back(4);
vector.push_back(3);
LecagyFunction(&vector[0], vector.size());
}
Run Code Online (Sandbox Code Playgroud)
预期的输出是:
5
4
3
Run Code Online (Sandbox Code Playgroud) 我有一个chars向量,其中包含一些数据元素。
std::vector<unsigned char> data_1;
Run Code Online (Sandbox Code Playgroud)
我有一个未签名的char *,它指向另一组数据元素。
unsigned char * data_2;
Run Code Online (Sandbox Code Playgroud)
问题:
有没有一种方法可以合并data_2到data_1向量中而根本不引起数据元素的复制?
我读到了本次讨论中提到的移动语义,但是我不确定在这种情况下是否有可能。