Questions tagged [stdvector]

std::vector is a contiguous sequence container in the C++ standard library. Its storage is handled automatically, so appending elements or resizing may cause the vector to allocate more memory. Use this tag for questions about std::vector or involving a std::vector.

Filter by
Sorted by
Tagged with
0 votes
0 answers
56 views

Wiring parents/children in a direct acyclic graph in C++ with smart pointers causes SIGSEGV [duplicate]

I'm trying to build a DAG in C++20. Each node has several children as well as parents. As a MWE, each node might compute some value. Coming from the Java world, this code works as desired: import java....
John Doe's user avatar
  • 800
0 votes
1 answer
147 views

Why don't raw pointer in vector become nullptr? [duplicate]

I want to dynamically-allocate an int and then push the corresponding pointer into the vector. After use, I set to nullptr all the elements of vector, but original pointer doesn't become nullptr. Why ...
sparrow's user avatar
0 votes
1 answer
91 views

List-initialization of vector of pairs

Note: Please don't close the question just because the root cause was due to comma operator. The value of the question was to let the community understand the failure of: std::vector<std::pair<...
cpp's user avatar
  • 297
-3 votes
0 answers
41 views

const store’ as ‘this’ argument discards qualifiers [-fpermissive] ERROR [duplicate]

#include<iostream> #include<stdlib.h> #include<fstream> #include<vector> #include<cstring> #include<string> using namespace std; class store{ public: string ...
Atreya Kamat's user avatar
1 vote
2 answers
77 views

How to ensure one time memory allocation while creating a std::vector from another container?

When I need to make an std::vector from some elements of another container, e.g. another vector, the safest way to ensure that the new vector will be allocated in memory only once is: std::vector<...
Damir Tenishev's user avatar
0 votes
0 answers
72 views

C++: contigouos memory for polymorphic objects

I'm currently working on a multiplayer board game and manage the playing fields using a simple std::vector<T>. However, I don't store the concrete objects in the vector, but only the pointers to ...
ThomasAlvaEdison's user avatar
11 votes
4 answers
946 views

Can one forward-declare a function taking a vector of incomplete type with a default value?

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, and ...
Fedor's user avatar
  • 17.6k
-2 votes
2 answers
96 views

I'm having a problem with pop_back() in c++

I'm trying to write a program in C++ that can swap the vocals on a sentence (by reversing it). #include <iostream> #include <string> #include <vector> int main() { // declare ...
rafiafnaanf's user avatar
0 votes
1 answer
90 views

C++ how to get the first and the last element in an std::vector by its base memory address

To me, a vector layout looks something like this: struct MyViewOnHowVectorLooksLike { public: int* _first, * _last, * _end; }; Known Parameters: Address of the std::vector<Type> object size ...
Jerry's user avatar
  • 11
0 votes
0 answers
28 views

I'm trying a program of setZeroes() in an mXn matrix by using vector and I get the following error-

Line 1037: Char 9: runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator>' (stl_vector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../...
Cocon Pappilon's user avatar
-1 votes
1 answer
137 views

c++ vector = {vec1.begin, vec1.begin} returns empty vector

I'm trying to understand the behavior of creating a subvector of another vector. For example, this code: https://godbolt.org/z/PYG34vnTr has a 2-element vector vec1. I want to create a vector of the ...
jkang's user avatar
  • 511
0 votes
1 answer
83 views

std::vector throws std::out_of_range while reading tokens from a file

I have a problem where I open a file (tokens.tk), I extract the tokens and put them into a Tokens struct. The problem I'm getting is when I'm trying to access the vector, it throws a std::out_of_range ...
JACK SUMMERS's user avatar
2 votes
1 answer
115 views

Is it best practice to assign a std::vector<T> to a variable before iteration?

I'm working with a smart pointer which returns a std::vector<T> over which I want to iterate using a for loop. Are there any disadvantages to returning the std::vector<T> directly within ...
Lee Mac's user avatar
  • 15.7k
0 votes
0 answers
41 views

comparator for c++ sort function giving errors [duplicate]

Following is solution of leetcode problem called Merge Intervals using compare function inside a sorting function class Solution { public: static bool compare(vector<int> t1,vector<int> t2)...
Raktim Majumdar's user avatar
0 votes
1 answer
64 views

Segmentation Fault while calling push_back on std::vector

Below I have provided a minimal working example of the error I am receiving. At a high level, the application segmentation faults at the line where I attempt to push_back a Point object into the ...
singular's user avatar
1 vote
1 answer
111 views

vector max size force test

I was testing the limits of the vector, and the below code always gives bad alloc error around the number 200 ish million elements where the vector max size is way way way bigger than that and I'm ...
Monogeon's user avatar
  • 366
2 votes
1 answer
64 views

Cast vector data of unique_ptr to pointer to const pointer

I have a std::vector of std::unique_ptr and I want to call a function from an API that takes a pointer to const pointer. Would it be correct to cast its data() to that type? This example program seems ...
jdehesa's user avatar
  • 58.7k
1 vote
2 answers
174 views

Unable to see gains on using boost::multi_array as compared to raw C array of pointers or C++ vector<vector<double>>

I am running a simple benchmarking exercise of 2d array access of reading and writing. I have const int XDim = 1000; and const int YDim = 1000; and create a 1000 x 1000 matrix. In C-style, I ...
Tryer's user avatar
  • 3,704
0 votes
1 answer
26 views

how to convert INT_MIN from int type to vector

vector<int> findSubarray(int a[], int n) { int pre=1; int suff=1; int ans=INT_MIN; for(int i=0;i<n;i++){ if(pre==0){ pre=0; } if(suff==0){ ...
Akshay Nimse's user avatar
-2 votes
1 answer
56 views

class of vector based on std::vector [duplicate]

I want to make my own vector class basing on std::vector. Everything seems to be easy, but I get an error even from the constructor. The class (minimum example) is as follow: #include<vector> ...
Antonella Longo's user avatar
0 votes
1 answer
63 views

Passing vector by reference in OpenMP loop

Is it safe to update a vector passed by reference in an OpenMP loop? In this example, I update the vector out_vector from a function inside the parallel loop to which it is passed by reference. #...
user's user avatar
  • 1,037
0 votes
1 answer
63 views

Does std::move guarantee vector.data() does not change?

Consider the following code: #include <vector> #include <utility> #include <cstdint> #include <bit> template <typename T> std::pair<std::vector<uint8_t>, T*> ...
SupAl's user avatar
  • 757
-2 votes
2 answers
127 views

Why std::vector doesn't use memcpy or realloc when resizing

I learnt that std::vector would explicitly call each element's copy-constructor during resizing, which if I understand correctly implies that it doesn't use memcpy or realloc under the hood, which was ...
simd's user avatar
  • 1,881
1 vote
2 answers
181 views

std::copy() vs memcpy() with std::vectors and fftw_malloc

I know when dealing with std::vector, it's best to use std::copy(), however I was wondering how to copy an fftw_malloc array into a std::vector? Should I use std::copy() or memcpy()? Example code: ...
Jamie 's user avatar
  • 441
1 vote
2 answers
65 views

Vector of class object

I was wondering how to create a vector of class object with custom constructors, though I ran into a problem with std::vector.push_back(), how am i supposed to create a new element in the vector with ...
nyedoardo's user avatar
2 votes
2 answers
201 views

Is using begin() and end() of an empty std::vector well defined?

Presume that I am copying a std::vector into a std::vector with std::vector::insert (as below). The question is: when the vector that I am copying from is empty, are begin() and end() well defined in ...
Timothy John Laird's user avatar
2 votes
2 answers
112 views

How to iterate over a range in descending order without modifying the range

Can someone tell me what I am doing wrong? I am trying to get access to elements in descending order. I cannot modify input data, so std::sort is not an option. I've tried to use std::max_element, but ...
mr__orangee's user avatar
0 votes
0 answers
89 views

How to improve replacement of elements in a std::vector?

I am working on the code shown below. Basically I have a global variable, called gParams, that is a std::vector of a data type I defined, called Parameter. Parameter is a struct with 3 fields. There ...
CepBoy's user avatar
  • 25
-3 votes
1 answer
114 views

why does my c++ program not work when I trying to print an element of a vector? [closed]

My c++ programs are not working when using vectors. The code of my program is given below, I am able to compile the file but when I try to run it just throws an error. #include <iostream> #...
Aqdas Rafat Khan's user avatar
2 votes
2 answers
101 views

Vector of Base Shared Pointers to Vector of Derived Shared Pointers

I have a class Base: class Base { }; and a derived class Derived: class Derived: public Base { }; Now I need to get a vector of Derived Pointers: std::vector<std::shared_ptr<Derived>> ...
Jan Hackenberg's user avatar
-2 votes
2 answers
44 views

two pointer error: whenever i use while(left<=right) it gives an error, but when i remove the equal sign it works fine

vector<int> v{-10,-7,-3,2,5,6}, v1(6); int left = 0; int right = 5; int i=0; while (left<=right){ if (abs(v[left])>abs(v[right])){ v1[i]=v[left]*v[left];...
Jeet Majumdar's user avatar
1 vote
3 answers
109 views

Constrain the key and value inner type of a nested std::map

Originally, I wanted to constrain the inner type of a nested std::vector, to be, for example, an int or a std::string. I was able to achieve this using C++20's concepts and the following stack ...
HarryP2023's user avatar
1 vote
3 answers
138 views

iteration through a very, very long std::vector question

I have a std::vector<std::tuple<int, int>> which shall be able to accommodate potentially very many elements. I do not know in advance how many elements it will have. It depends on how I ...
velenos14's user avatar
  • 534
6 votes
1 answer
112 views

braced initialization of std::vector of move-only objects fails [duplicate]

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 doesn't seem to work is ...
Don Hatch's user avatar
  • 5,121
3 votes
2 answers
174 views

How can I order a vector using another vector?

This question is similar to the one posed here however, this answer does not work for my question and it is slightly different. What I am trying to do would best be shown in code: //this would be a ...
Sam Moldenha's user avatar
1 vote
1 answer
106 views

Range checking for operator[] with std::vector and std::array in debug mode

I'm writing some numerical C++ where both std::vector and std::array are being used in performance critical parts of the code. Preferably I would like the operator[] to do range checking in debug mode ...
ander's user avatar
  • 11
4 votes
2 answers
141 views

Will the std::vector copy assignment operator avoid releasing and reallocating memory if possible?

I have a function copying one vector to another (among many other things). A simplified version of the code is below. void Fun(std::vector<double> &in, std::vector<double> &out) { ...
Mark Mc's user avatar
  • 99
-1 votes
1 answer
102 views

What prevents the subclassing of vector<> and having at() or operator[] resize the array if necessary?

What prevents the subclassing of vector<> and overriding at() or operator[] to resize the array if necessary? Specifically I'd like code to work like this: MYvector<int> v; v[3] = 1; ...
Swiss Frank's user avatar
  • 2,115
1 vote
1 answer
101 views

What should be the definition for the concept of `vector` element type?

I am trying to implement vector to practice C++ and use C++20 concept to constrain vector's element type. What should be the definition of concept (type constraints) for vector element type? I think ...
Waker's user avatar
  • 226
6 votes
1 answer
135 views

Does the type of std::vector<int>::iterator change when you increment it with '++'?

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 ...
Dave Woodward's user avatar
0 votes
2 answers
72 views

split a string by a vector of strings

I have an input string, and I also have a vector of separators. I want to output the vector of strings that are not in the separators and as an additional entry, tje separator that was found So for ...
markt1964's user avatar
  • 2,666
3 votes
3 answers
217 views

Are elements of a std::vector stored separately in memory?

Question : I tried to print the number of byte and the memory address of elements that are in a vector. The result, I see a memory address and a number of byte. Are the elements of a vector stored ...
Meniev's user avatar
  • 148
2 votes
0 answers
57 views

Using cython to convert an existing C++ function that takes std::vector and output std::vector

I have a C++ file that takes std::vector and output std::vector, how do I convert it in cython ? C++ file: cpp_func.cpp std::vector<std::vector<float>> my_cpp_function ( const size_t& ...
Ong Beng Seong's user avatar
1 vote
0 answers
69 views

Compilation warning: Vector _M_end_of_storage may be used uninitialized when using list initialization for nested structs containing vectors

I noticed this warning when I tried to use list initialization to initialize nested structs which contain vectors. While the code seems to perform as expected during runtime, I'm not sure why I am ...
Fennel Foxxo's user avatar
1 vote
1 answer
96 views

Significant performance difference between iterator-based loop and index-based loop in C++ with SFML

I'm using SFML in C++ to develop a simple 2D grid visualization game engine. I have a Grid class with a std::vector<sf::Vertex> where each cell in the grid is represented by 6 vertices. When I ...
AK-35's user avatar
  • 589
0 votes
0 answers
26 views

How to use getline alternative in C++ for delimiters with more than one character? [duplicate]

I know I can't use getline when I want to delimit on more than one character, so what do I use in its place? For this code example, I want to use two pipe symbols || instead of one pipe symbol |. I ...
JeffR's user avatar
  • 795
1 vote
1 answer
151 views

Unexpected compile error when dealing with vector of unique_ptr<pure_virtual_class> inside std::map

#include <iostream> #include <algorithm> #include <vector> #include <map> class Base { public: virtual ~Base() = default; virtual void doIt() = 0; public: int base = 1; ...
undersilence's user avatar
-2 votes
2 answers
81 views

Use a vector of pairs as an unordered_map [closed]

I initially started out by using an unordered_map to store the frequencies of a certain number appearing and then later using a priority_queue to implement a max heap in order to get the first element ...
Sarcana's user avatar
  • 15
1 vote
0 answers
161 views

Is `std::vector` guaranteed to steal resource on move construction?

Take the simple snippet: void test(std::vector<int> && vec){ auto const ptr = data(vec); auto new_vec {std::move(vec)}; (void)*ptr; // UB or not? }; Is using pointer ...
Red.Wave's user avatar
  • 3,175
0 votes
3 answers
151 views

How to use original index of a string to erase single character?

Assume that I have a string 'abcd', and a vector [4,1,3,2] to index the string. For example, first element of vector is 4, so I should remove 4th character in 'abcd' which refers to 'd'. Then the ...
Sonny's user avatar
  • 11

1
2 3 4 5
59