Difference between revisions of "Talk:cpp/container/vector"
T.. Canens (Talk | contribs) m (Redirected page to Playground) |
m (Reverted edits by T.. Canens (talk) to last revision by Cubbi) |
||
Line 1: | Line 1: | ||
− | # | + | --[[User:BohdanKornienko|BohdanKornienko]] ([[User talk:BohdanKornienko|talk]]) 13:23, 16 October 2013 (PDT) |
+ | |||
+ | Small example to use vector header: | ||
+ | |||
+ | {{c|#include <iostream> | ||
+ | #include <vector> | ||
+ | |||
+ | int main(int argc, char **argv) | ||
+ | { | ||
+ | std::vector<int> vec; | ||
+ | |||
+ | vec.insert(vec.begin(), 1); | ||
+ | vec.insert(vec.end(), 2); | ||
+ | vec.insert(vec.end(), 3); | ||
+ | |||
+ | vec.erase(vec.begin() + 1); | ||
+ | |||
+ | std::cout << "count: " << vec.size() | ||
+ | << "\ncapacity: " << vec.capacity() << std::endl; | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | }} | ||
+ | |||
+ | == Pointer/iterator invalidation and vector::assign. == | ||
+ | |||
+ | Does vector::assign invalidate iterators? It's not on the list, but I have to assume that if the new size blows the capacity, they have to be invalidated, right? What does the standard say about this? | ||
+ | |||
+ | : Neither the simplified summary here nor the actual page ({{lc|std::vector::assign}}) say anything about invalidation because the standard (Table 107 — Sequence container requirements) does not say anything about invalidation; it just says "Replaces elements ... with copies of". For input iterators, LLVM libc++ just calls [https://github.com/llvm-mirror/libcxx/blob/master/include/vector#L1372 clear and then a series of push_backs], obviously invalidating everything. GNU libstdc++ [https://github.com/gcc-mirror/gcc/blob/edd716b6b1caa1a5cb320a8cd7f626f30198e098/libstdc%2B%2B-v3/include/bits/vector.tcc#L249 is a little more careful] --[[User:Cubbi|Cubbi]] ([[User talk:Cubbi|talk]]) 12:09, 22 April 2016 (PDT) | ||
+ | :: If there's no wording anywhere, I think we'll need an LWG issue, because otherwise the blanket wording in [http://eel.is/c++draft/container.requirements.general#12 <nowiki>[container.requirements.general]/12</nowiki>] kicks in, which would be obviously wrong. [[User:T. Canens|T. Canens]] ([[User talk:T. Canens|talk]]) 14:41, 22 April 2016 (PDT) | ||
+ | :::Ah, it was implied by the old specification pre-[http://wg21.link/LWG2209 LWG2209], but that issue's resolution removed it. [[User:T. Canens|T. Canens]] ([[User talk:T. Canens|talk]]) 15:41, 24 April 2016 (PDT) | ||
+ | |||
+ | == Wrong description of operator[] == | ||
+ | |||
+ | It currently describe operator[] as "access or insert specified element", which is incorrect, since std::vector::operator[] never inserts an element, unlike std::map::operator[]. This is because the description uses the standard operator_at template. I'd argue it's better to give a correct description. [[Special:Contributions/2620:0:105F:2:6169:A7F:B58D:284B|2620:0:105F:2:6169:A7F:B58D:284B]] 01:23, 29 January 2018 (PST) | ||
+ | : fixed --[[User:D41D8CD98F|D41D8CD98F]] ([[User talk:D41D8CD98F|talk]]) 01:29, 29 January 2018 (PST) | ||
+ | |||
+ | == The infamous vector<bool> == | ||
+ | |||
+ | It's mentioned in the man page but not on any of the member pages. | ||
+ | Also, It would be nice if there was more on the main page explaining the limitations - In particular: | ||
+ | data() does not exist | ||
+ | and &v[0] is not a pointer to bool. | ||
+ | |||
+ | Just noticed that it has its own page but should still probably be referenced from some of the member pages | ||
+ | |||
+ | [[Special:Contributions/194.74.130.171|194.74.130.171]] 02:06, 19 April 2021 (PDT) | ||
+ | |||
+ | : In addition, {{tt|std::vector<bool>::resize}} has only one [https://timsong-cpp.github.io/cppwp/n4861/vector.bool overload]: | ||
+ | {{source|1=constexpr void resize(size_type sz, bool c = false);}} | ||
+ | whilst {{ltt|cpp/container/vector/resize|std::vector<T>::resize()}} has two [https://timsong-cpp.github.io/cppwp/n4861/vector overloads]: | ||
+ | {{source|1= | ||
+ | constexpr void resize(size_type sz); | ||
+ | constexpr void resize(size_type sz, const T& c); | ||
+ | }} | ||
+ | Thus, we need to {{todo|describe {{tt|std::vector<bool>::resize()}} separately.}} | ||
+ | : --[[User:Space Mission|Space Mission]] ([[User talk:Space Mission|talk]]) 05:43, 19 April 2021 (PDT) |
Latest revision as of 07:33, 9 October 2023
--BohdanKornienko (talk) 13:23, 16 October 2013 (PDT)
Small example to use vector header:
#include <iostream>
#include <vector>
int main(int argc, char **argv)
{
std::vector<int> vec;
vec.insert(vec.begin(), 1);
vec.insert(vec.end(), 2);
vec.insert(vec.end(), 3);
vec.erase(vec.begin() + 1);
std::cout << "count: " << vec.size()
<< "\ncapacity: " << vec.capacity() << std::endl;
return 0;
}
[edit] Pointer/iterator invalidation and vector::assign.
Does vector::assign invalidate iterators? It's not on the list, but I have to assume that if the new size blows the capacity, they have to be invalidated, right? What does the standard say about this?
- Neither the simplified summary here nor the actual page (std::vector::assign) say anything about invalidation because the standard (Table 107 — Sequence container requirements) does not say anything about invalidation; it just says "Replaces elements ... with copies of". For input iterators, LLVM libc++ just calls clear and then a series of push_backs, obviously invalidating everything. GNU libstdc++ is a little more careful --Cubbi (talk) 12:09, 22 April 2016 (PDT)
- If there's no wording anywhere, I think we'll need an LWG issue, because otherwise the blanket wording in [container.requirements.general]/12 kicks in, which would be obviously wrong. T. Canens (talk) 14:41, 22 April 2016 (PDT)
[edit] Wrong description of operator[]
It currently describe operator[] as "access or insert specified element", which is incorrect, since std::vector::operator[] never inserts an element, unlike std::map::operator[]. This is because the description uses the standard operator_at template. I'd argue it's better to give a correct description. 2620:0:105F:2:6169:A7F:B58D:284B 01:23, 29 January 2018 (PST)
- fixed --D41D8CD98F (talk) 01:29, 29 January 2018 (PST)
[edit] The infamous vector<bool>
It's mentioned in the man page but not on any of the member pages. Also, It would be nice if there was more on the main page explaining the limitations - In particular: data() does not exist and &v[0] is not a pointer to bool.
Just noticed that it has its own page but should still probably be referenced from some of the member pages
194.74.130.171 02:06, 19 April 2021 (PDT)
- In addition,
std::vector<bool>::resize
has only one overload:
constexpr void resize(size_type sz, bool c = false);
whilst std::vector<T>::resize() has two overloads:
constexpr void resize(size_type sz); constexpr void resize(size_type sz, const T& c);
This section is incomplete Reason: describe std::vector<bool>::resize() separately. |
- --Space Mission (talk) 05:43, 19 April 2021 (PDT)