stdvector Questions

2

This is a follow up to this question, Restricted access for allocated arrays belonging to separate object, after discovering that returning by value across compilation units doesn't help to hint th...
Fauch asked 21/10 at 18:23

3

Solved

I noticed that the parameter of vector<T>::push_back is const T&. I know that a reallocation may take place when I call push_back, but vector<T>::back returns a reference. So ...
Joyce asked 7/9 at 9:18

3

Solved

I want to get a range of elements from a std::vector<MyClass> and store them as const-ref, because I only want to read but not modify them. #include <iostream> #include <vector> ...
Sweitzer asked 6/9 at 7:43

2

The "four basic kinds" of C++ sequence containers (vector, forward_list, list, and deque) all have an assign method. For example, std::vector has (quoting cppreference.com): void as...
Lyric asked 4/8 at 19:51

1

I know there are ways to either do or not do bounds-checking when indexing a vector, but specifically on push_back(), even if I know the capacity of the vector is large enough (i.e., I reserved eno...
Punner asked 25/7, 2017 at 21:48

4

I have a vector of bytes (std::vector ): [1,1,1,9,9,9,1,1,1] I want to replace bytes 3-6 with unknown length of bytes (I'll know at run time obv.). So the length of the vector may grow, shirk or s...
Reflation asked 13/3, 2017 at 11:50

4

I have Example Class: class Example { private: int testValue1; int testValue2; int testValue3; public: Example(int pVal1, int pVal2, int pVal3); Example(const Example); const Example oper...
Durkheim asked 17/10, 2013 at 11:32

3

Solved

Language: C++ One thing I can do is allocate a vector of size n and store all data and then sort it using sort(begin(),end()). Else, I can keep putting the data in a map or set which are ordered i...
Revitalize asked 6/5, 2018 at 10:49

1

Solved

Overloading the operator bool() for a custom class T breaks std::vector<T> comparison operators. The following code tried on the first online compiler google suggest me prints v1 > v2: 0 v...
Avid asked 18/4 at 10:38

1

Solved

I noticed that the std::vector container's swap function has a different noexcept-specification than all other containers. Specifically, the function is noexcept if the expression std::allocator_tr...
Shadowy asked 10/5, 2023 at 19:44

3

Solved

Why when I init std::vector with braces std::vector<TS> vec {ts1, ts2}; Compiler call twice copy constructor operator? On the other hand - with push_back it called only once. #include &lt...
Sonni asked 10/12, 2013 at 17:48

4

Solved

I wanted to do this #include <vector> #include <span> struct S { std::vector<int> v; void set(std::span<int> _v) { v = _v; } }; But it does not compile. What are the ...
Ehrman asked 1/9, 2020 at 10:46

3

Given a C++ vector (let's say it's of doubles, and let's call it unsorted), what is the most performant way to create a new vector sorted which contains a sorted copy of unsorted? Consider the fol...
Uticas asked 20/3, 2017 at 23:23

3

Solved

I write tools to dump and load common objects in a binary file. In a first quick implementation, I wrote the following code for std::vector<bool>. It works, but it is clearly not optimized in...
Deficiency asked 14/4, 2015 at 9:19

1

Solved

I have the following code: #include <print> #include <vector> int main() { std::vector<int> v{1, 2, 3}; std::println("{}", v); } Among the numerous errors this produ...
Rage asked 4/1 at 0:44

4

Solved

I have a std::vector<std::string> v; (initialized). How can I use the range-for loop for accessing all elements except the first one (on index zero). For all elements: for (const string &amp...
Zendah asked 11/8, 2015 at 8:28

4

Solved

I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as I go. If I successfully delete the file, I remove it from the vector. My questio...
Erhart asked 22/10, 2009 at 1:37

2

Solved

Is the following code ok?: std::vector<char> var; size_t requiredSize; getenv_s(&requiredSize, NULL, 0, "Something"); if (requiredSize == 0) { return ENV_NOT_EXIST; } if(var.size() &lt...
Convexoconcave asked 12/9, 2013 at 8:52

4

Solved

The code snippet below demonstrates a real issue I faced recently in my program: #include<vector> class A; void f( const std::vector<A> & = {} ); There is an incomplete class A, ...

20

Solved

All I want to do is to check whether an element exists in the vector or not, so I can deal with each case. if ( item_present ) do_this(); else do_that();
Listel asked 20/2, 2009 at 21:58

5

Solved

if I do: std::unique_ptr<int[]> uparr(new int[1984]); and I pass uparr to somebody without passing the 1984 to them can they see how many elements it has? aka is there equivalent of vector...
Moralize asked 26/3, 2014 at 13:21

4

Solved

I'm trying to read a file in binary format into a std::vector<std::byte> std::ifstream fStream(fName, std::ios::binary); std::vector<std::byte> file_content((std::istreambuf_iterato...
Como asked 24/2, 2018 at 16:22

1

I can concisely (with braces) initialize 5 out of 6 of the following cases: of copyable of move-only array YES YES std::array YES YES std::vector YES NO The one case that doe...
Fabri asked 14/8, 2023 at 7:6

33

Solved

How do I print out the contents of a std::vector to the screen? A solution that implements the following operator<< would be nice as well: template<container C, class T, String delim = &q...
Anchovy asked 25/5, 2012 at 7:13

33

Solved

How do I print out the contents of a std::vector to the screen? A solution that implements the following operator<< would be nice as well: template<container C, class T, String delim = &q...
Acetal asked 25/5, 2012 at 7:13

© 2022 - 2024 — McMap. All rights reserved.