我的问题很简单:std :: vector元素是否保证是连续的?在order word中,我可以使用指向std :: vector的第一个元素的指针作为C数组吗?
如果我的记忆力很好,那么C++标准就没有这样的保证.但是,如果元素不连续,那么std :: vector要求几乎不可能满足它们.
有人可以澄清一下吗?
例:
std::vector<int> values;
// ... fill up values
if( !values.empty() )
{
int *array = &values[0];
for( int i = 0; i < values.size(); ++i )
{
int v = array[i];
// do something with 'v'
}
}
Run Code Online (Sandbox Code Playgroud) 我只是习惯了使用std :: auto_ptr的智能指针.
假设我想用auto_ptr和普通指针调用一个函数.
auto_ptr<uint32> data_smart(new uint32[123])]);
uint32 data_fix[123];
uint32* data_dumb = new uint32[123];
processData(data_smart);
processData(data_fix);
processData(data_dumb);
Run Code Online (Sandbox Code Playgroud)
没有超载的最佳做法是什么?使用带有uint32*参数的processData函数?我可以使用.get()将智能指针强制转换为uint32*吗?或者我应该怎么做?提前致谢!
为什么std :: auto_ptr上不允许使用operator []?
#include <iostream>
using namespace std ;
template <typename T>
void foo( T capacity )
{
auto_ptr<T> temp = new T[capacity];
for( size_t i=0; i<capacity; ++i )
temp[i] = i; // Error
}
int main()
{
foo<int>(5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在Microsoft Visual C++ 2010上编译.
错误:错误C2676:二进制'[':'std :: auto_ptr <_Ty>'未定义此运算符或转换为预定义运算符可接受的类型