Linked Questions

4 votes
3 answers
18k views

The size() for vectors in C++ standard library returns zero The size() is supposed to return the current number of elements. Is something wrong with my code ? Or is it a bug in the function ? It ...
NB2345's user avatar
  • 71
-3 votes
2 answers
7k views

What does vector::reserve actually do since trying to access its data is an error? vector<int> v.reserve(10); v[4] = 22; if this allocated space it shouldn't be an error in the first place.. ...
Dean's user avatar
  • 7,034
2 votes
3 answers
2k views

I wrote the following code to test the value of capacity of a vector when it was constructed from another. This was inspired by a question asked here. #include <iostream> #include<vector> ...
kaartic's user avatar
  • 523
2 votes
1 answer
3k views

I need to fill a vector in with particular values. I find that the below code works, except in that a.size() fails to change from 0. Adding a resize call after I put in the elements takes almost twice ...
David Lerner's user avatar
1 vote
2 answers
1k views

I have to make a vector that stores char pointers. Each pointer should only point to a single char like 'a' or 'b'. I don't get any compile errors, but when I run my program, it crashes. I cannot use ...
The_Redhawk's user avatar
3 votes
2 answers
476 views

What's wrong here? std::vector<std::vector<int>> mSectionsSubsets; int count = (int)powf(2, NUM_SECTIONS); mSectionsSubsets.reserve(count); for (int i = 0; i < count; i++) { ...
markzzz's user avatar
  • 48.3k
0 votes
1 answer
1k views

If I have a class's header file like the following: class arrayStack { private: struct StackNode { StackNode(int p, int v):previous(p),value(v){} ~StackNode(){std::cout<<...
user268451's user avatar
0 votes
1 answer
293 views

I have a class template <class T> class General_matrix : public Math_object<T> { public: General_matrix(const size_t m, const size_t n); ~General_matrix(); ...
 ProkopenkoV's user avatar
0 votes
1 answer
210 views

I was unable to make std::copy works. I tried several codes in internet, but I was unable to make it work. I need to use copy, first, to understand why is not working, second, to use it in a ...
Werem's user avatar
  • 594
1 vote
1 answer
227 views

Why this program crashes? It crashes on line vec[0].assign("blabla");: #include <vector> #include <string> using namespace std; int main() { vector<string> vec; vec....
Scarass's user avatar
  • 954
2 votes
1 answer
87 views

Snippet1: The following snippet prints out 0 1 but returns an empty vector. vector<int> trial() { vector<int> ret; ret.reserve(2); ret[0] = 0; ret[1] = 1; cout <&...
user3828311's user avatar
1 vote
2 answers
78 views

I initialized a vector in my c++ program, then reserved some memory to store values in it. Why I can't assign value to the memory block I had reserved earlier? vector<int> test; test.reserve(100)...
Firegreat's user avatar
-1 votes
2 answers
132 views

What's the difference between std::vector<uint8_t> vec1(size); and std::vector<uint8_t> vec2; vec2.reserve(size); Do they both allocate memory on heap?
mewo1234's user avatar
  • 319
0 votes
1 answer
119 views

While working on a submission, I found behaviour that I don't understand. I have three ways of populating my matrix from the input. One of them works, one of them compiles and runs but produces a ...
lucidbrot's user avatar
  • 6,552
0 votes
1 answer
133 views

Consider the simple code: I reserve memory for 2d vector, import the module in python, make instance of class and run the python code once. The second run of the python code make an error: #include &...
Abolfazl's user avatar
  • 1,235

15 30 50 per page