Преобразование между станд. C++:: вектор и C выстраивают без копирования

Пойдите для SVN. Если Вы никогда не использовали управление исходным кодом прежде, оно не будет иметь значения для Вас так или иначе.

кроме того, нет большого объема изучения вовлеченного в использование Системы управления исходным кодом. Если Вы учитесь один, можно легко переключиться на другого позднее.

SVN является большим инструментом, и он должен заботиться о большинстве Ваших потребностей. И так как это было вокруг, это имеет справедливого акционера инструментов GUI (TortoiseSVN, например).

Идут для SVN.

54
задан James McNellis 18 September 2010 в 17:49
поделиться

3 ответа

You can get a pointer to the first element as follows:

int* pv = &v[0];

This pointer is only valid as long as the vector is not reallocated. Reallocation happens automatically if you insert more elements than will fit in the vector's remaining capacity (that is, if v.size() + NumberOfNewElements > v.capacity(). You can use v.reserve(NewCapacity) to ensure the vector has a capacity of at least NewCapacity.

Also remember that when the vector gets destroyed, the underlying array gets deleted as well.

85
ответ дан 7 November 2019 в 07:46
поделиться

If you have very controlled conditions, you can just do:

std::vector<int> v(4,100);
int* pv = &v[0];

Be warned that this will only work as long as the vector doesn't have to grow, and the vector will still manage the lifetime of the underlying array (that is to say, don't delete pv). This is not an uncommon thing to do when calling underlying C APIs, but it's usually done with an unnamed temporary rather than by creating an explicit int* variable.

16
ответ дан 7 November 2019 в 07:46
поделиться
int* pv = &v[0]

Note that this is only the case for std::vector<>, you can not do the same with other standard containers.

Scott Meyers covers this topic extensively in his books.

20
ответ дан 7 November 2019 в 07:46
поделиться