stdvector Questions

1

Solved

I'm experimenting with C++20 concepts and I've hit a strange problem I can't explain. I'm trying to define a concept that checks a given iterator can be incremented (fine), and also checks that the...
Lauer asked 3/8, 2023 at 13:50

7

Solved

What's the fastest way to reset every value of a std::vector<int> to 0 and keeping the vectors initial size ? A for loop with the [] operator ?
Vulgarity asked 13/1, 2012 at 9:46

5

Solved

vector<vector<int>> res; res.emplace_back({1,2}); // change to res.push_back({1,2}); would work This gives me the error: main.cpp:61:25: error: no matching function for call to ‘std::v...
Clearly asked 5/12, 2013 at 4:46

1

Solved

Since C++20, std::vector can be used in constant expressions. And as far as I known, current C++ permits dynamically allocate memory under the condition that any such allocation is deallocated by t...
Dele asked 11/6, 2023 at 11:15

7

Solved

I want to clear a element from a vector using the erase method. But the problem here is that the element is not guaranteed to occur only once in the vector. It may be present multiple times and I n...
Togo asked 7/12, 2008 at 10:10

29

How do I concatenate two std::vectors?
Miser asked 14/10, 2008 at 15:46

10

Solved

I like vectors a lot. They're nifty and fast. But I know this thing called a valarray exists. Why would I use a valarray instead of a vector? I know valarrays have some syntactic sugar, but other t...
Sofko asked 21/10, 2009 at 17:53

5

Solved

Could someone tell what is the correct way to work with a vector of arrays? I declared a vector of arrays (vector<float[4]>) but got error: conversion from 'int' to non-scalar type 'float [...
Messalina asked 6/1, 2011 at 6:11

4

Solved

In order to understand the memory consumption of std::vector<int> I wrote: std::cout << sizeof(std::vector<int>) << std::endl; This yields 32. I tried to understand where...
Printable asked 27/11, 2017 at 9:31

11

When I do this: std::vector<int> hello; Everything works great. However, when I make it a vector of references instead: std::vector<int &> hello; I get horrible errors like ...
Coterminous asked 28/5, 2009 at 18:4

9

Solved

I have an array of integers that I need to remove duplicates from while maintaining the order of the first occurrence of each integer. I can see doing it like this, but imagine there is a better wa...
Entangle asked 30/8, 2012 at 15:33

7

Solved

I had been going through the Book: C++ Primer, Third Edition By Stanley B. Lippman, Josée Lajoie, found 1 mistake in the program given under the Article 6.3 How a vector Grows Itself, this program ...
Rarotonga asked 8/3, 2011 at 12:6

2

Solved

Consider the following program: #include <vector> #include <iostream> class A { int x; public: A(int n) noexcept : x(n) { std::cout << "ctor with value\n"; } A(const...
Bangweulu asked 10/10, 2022 at 8:32

1

Solved

If I have two C++ vectors: vector<int> a(5) {1,2,3,4,5}; vector<int> b(5) {6,7,8,9,10}; is there a one-line way to use the swap method to swap slices of a and b? Something like swap(a[...
Coeval asked 30/9, 2022 at 21:58

16

Solved

I have a std::vector<int>, and I want to delete the nth element. How do I do that? Example: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); vec.erase(??...
Glomerulus asked 17/5, 2009 at 17:59

1

I couldn't understand the syntax of the way the vector root has been initialized with size sz in the constructor - UnionFind(int sz) : root(sz) class UnionFind { public: UnionFind(int sz) :...
Luker asked 1/9, 2022 at 2:55

13

Solved

So, I have the following: std::vector< std::vector <int> > fog; and I am initializing it very naively like: for(int i=0; i<A_NUMBER; i++) { std::vector <int> fogRow; for(int...
Kipton asked 15/7, 2013 at 20:21

9

Solved

How much data is copied, when returning a std::vector in a function and how big an optimization will it be to place the std::vector in free-store (on the heap) and return a pointer instead i.e. is:...
Lancewood asked 29/3, 2013 at 13:46

8

Solved

I know that manual dynamic memory allocation is a bad idea in general, but is it sometimes a better solution than using, say, std::vector? To give a crude example, if I had to store an array of n ...
Enrollee asked 8/3, 2013 at 12:35

5

Solved

I have a single even-sized vector that I want to transform into a vector of pairs where each pair contains always two elements. I know that I can do this using simple loops but I was wondering if t...
Xenon asked 14/2, 2022 at 13:32

1

Solved

I'm reading A Tour of C++ (2nd edition) and I came across this code (6.2 Parameterized Types): template<typename T> T* end(Vector<T>& x) {     return x.size() ? &x[0]+x.size() ...
Mate asked 19/5, 2022 at 10:17

3

Solved

For example, I have such struct in a templated class: struct Foo{ int data; vector<Foo*> children; } And to print out the data value, I can simply do this: (let bar be a pointer to a Foo...
Ammoniacal asked 12/4, 2015 at 8:16

2

There are several ways to make a vector empty, including std::vector<int> example = makeMyReallyBigVector(); example.clear(); example.resize(0); example.erase(example::being(), example::end()...
Macy asked 18/2, 2022 at 9:2

2

The following question is related, however answers are old, and comment from user Marc Glisse suggests there are new approaches since C++17 to this problem that might not be adequately discussed. ...
Loveinidleness asked 11/2, 2020 at 13:19

1

Solved

The following code does fail on MSVC but compiles on GCC and Clang, godbolt #include <unordered_map> #include <vector> using namespace std; struct NonCopyable { NonCopyable() = defaul...
Injurious asked 22/12, 2021 at 23:31

© 2022 - 2024 — McMap. All rights reserved.