Vector <int> input and output - c++

I am stuck with simple vector input and output operations. The compiler returns error saying 'std::outof range'
Here is the code
int main()
{
int size;
cout <<"Enter size of vector\n";
cin>>size;
cout<<"Now to input the vector of size "<<size<<endl;
vector <int> trial;
for (size_t i=0;i<size;++i){
int x;
cout<<"write at position"<<trial.at(i)<<'t';
cin>>x;
trial.push_back(x);
cout<<endl;
}
ostream_iterator<int> output(cout,"");
copy(trial.begin(),trial.end(),output);
}
I would appreciate a brief explanation of the internal workings of the problem.

You invoke trial.at(i) before trial.push_back(x), accessing a not yet existing element. Since the element doesn't (yet) exist, i is an invalid index, and at() will throw a std::out_of_range exception when passed an invalid index. If an exception isn't caught, it will terminate the program. Presumably your platform's runtime library displays the exception that caused the program to be terminated.
I suppose what you actually want is this:
std::cout << "write at position " << i << '\t';

Consider the first iteration of this loop:
vector <int> trial;
for (size_t i=0;i<size;++i){
int x;
cout<<"write at position"<<trial.at(i)<<'t';
At the first iteration nothing has been pushed into the vector, so trial.at(0) isn't yet valid. The result will be an exception. Since you don't have a try/catch anywhere, that will end your program's execution.
It looks to me like you want cout << "write at position " << i; instead. i is the position; after it has been pushed onto the vector so it's valid, vector.at(i) will be the value at that position.

trial.at(i)
You're accessing an element that doesn't exist.
You probably want cout<<"write at position"<< i <<'t'; anyway.

The problem is this line:
cout<<"write at position"<<trial.at(i)<<'t';
You call this before you have set the size of the vector. I'm not really sure what that line is trying to accomplish anyway. If you're trying to print the in memory position (address) that won't do it, if you're trying to print what was already there then it would work if it had already been allocated. Using vector::push_back() means that you don't need to preallocate though.
You can fix this by resizing the vector and accessing the elements directly like this:
trial.resize(size);
// loop
// vector.push_back(x) -- now becomes
vector[i] = x;
Or, you can simply remove the printing of the position and use push_back() as you are now.
Since it seems you're investigating how to use vector I would suggest trying to gain an understanding of how the push_back() and resize() methods differ, and have also have a look at the vector::reserve() function.

Related

Vector of pointers undefined behaviour

I'm trying to make a vector of pointers whose elements are pointing to vector of int elements. (I'm solving a competitive programming-like problem, that's why it sounds kinda nonsense).
but here's the code:
#include <bits/stdc++.h>
using namespace std;
int ct = 0;
vector<int> vec;
vector<int*> rf;
void addRef(int n){
vec.push_back(n);
rf.push_back(&vec[ct]);
ct++;
}
int main(){
addRef(1);
addRef(2);
addRef(5);
for(int i = 0; i < ct; i++){
cout << *rf[i] << ' ';
}
cout << endl;
for(int i = 0; i < ct; i++){
cout << vec[i] << ' ';
}
}
When I execute the code, it's showing weird behaviour that I don't understand. The first element of rf (vector<int*>) seems not pointing to the vec's (vector<int>) element, where the rest of the elements are pointing to it.
here's the output when I run it on Dev-C++:
1579600 2 5
1 2 5
When I tried to run the code here, the output is even weirder:
1197743856 0 5
1 2 5
The code is intended to have same output between the first line and the second.
Can you guys explain why it happens? Is there any mistake in my implementation?
thanks
Adding elements to a std::vector with push_back or similar may invalidate all iterators and references to its elements. See https://en.cppreference.com/w/cpp/container/vector/push_back.
The idea is that in order to grow the vector, it may not have enough free memory to expand into, and thus may have to move the whole array to some other location in memory, freeing the old block. That means in particular that your pointers now point to memory that has been freed, or reused for something else.
If you want to keep this approach, you will need to resize() or reserve() a sufficient number of elements in vec before starting. Which of course defeats the whole purpose of a std::vector, and you might as well use an array instead.
The vector is changing sizes and the addresses you are saving might not be those you want. You can preallocate memory using reserve() and the vector will not resize.
vec.reserve(3);
addRef(1);
addRef(2);
addRef(5);
The problem occurs when you call vec.push_back(n) and vec’s internal array is already full. When that happens, the std::vector::push_back() method allocates a larger array, copies the contents of the full array over to the new array, then frees the old/full array and keeps the new one.
Usually that’s all you need, but your program is keeping pointers to elements of the old array inside (rf), and these pointers all become dangling/invalid when the reallocation occurs, hence the funny (undefined) behavior.
An easy fix would be to call vec.reserve(100) (or similar) at the top of your program (so that no further reallocations are necessary). Or alternatively you could postpone the adding of pointers to (rf) until after you’ve finished adding all the values to (vec).
Just do not take pointer from a vector that may change soon. vector will copy the elements to a new space when it enlarges its capacity.
Use an array to store the ints instead.

How does C++ handle vectors when passing from a function to main?

I'm seeing strange behaviour in the output of a 'simple' function that generates the binary representation of an integer. I aim to use the function in another code, but first want to see it working by printing to the console. I first define the function dec2bin to take a number and the length of the required bitstring. After defining a vector that will be returned by the function, the vector is populated with the required binary values. In the main function, the vector is accessed and printed element-wise to the console. However, the output is incorrect unless I include an arbitrary cout statement within the vector populating for loop.
The function is defined as follows:
vector<int> dec2bin(int N, int j){
vector<int> myvec;
myvec.push_back(N);
for (int i = N-1; i >= 0; i--) {
int r = j >> i;
if (r & 1)
myvec[N-i-1] = 1;
else
myvec[N-i-1] = 0;
//cout << " ";
}
return myvec;
}
My main function is:
int main(){
int length = 8;
int num = 15;
vector<int> myvec = dec2bin(length,num);
for (int j=0; j<length; j++){cout << myvec[j];}
return 0;
}
When the commented out line (cout << " ") is included within the for loop the output is correct, with 00001111. Without that line (or with the line outside of the for loop) the output is incorrectly 00001141130.
I have read various things about pointers and memory allocation, so expect this is where I am going wrong; however, I can't quite see how to incorporate those ideas into my own code. Any advice would be greatly appreciated, many thanks.
On this line:
myvec[N-i-1] = 1;
You are invoking undefined behavior for any index other than 0.
myvec.push_back(N); pushes a single element to the vector, and you never add more elements to the vector. When you access a vector out of bounds, you will not get a compiler error or runtime exception, but your code has undefined behavior, which means the output could be literally anything.
Actually, it seems like you just confused push_back() with resize(). And instead of resize(), you can pass the size to the constructor. If you replace:
vector<int> myvec;
myvec.push_back(N);
with:
std::vector<int> myvec(N);
or:
std::vector<int> myvec;
myvec.resize(N);
You get a vector with N elements, and accessing myvec[i] is fine for any index 0 <= i < N. Alternatively, you could call myvec.reserve(10) to let the vector allocate space for N elements. The vector will then still be empty, and you have to push_back() any element you want to add.
You are accessing the unallocated memory because you only pushed back(push_back()) once and due to it you are getting undefined behaviour.
You should use .at() function instead of [] operator for accessing the vector element, it'll throw the exception out of bounds.
You need to use push_back() for insertion into the vector or you can use vector.resize(size) during vector declaration then it'll save you from undefined behavior.
Thanx

implementation of stacks using vector - segmentation fault in peek() function

#include <iostream>
#include <vector>
using namespace std;
class Stack{
public:
vector<string> vc;
int length=0;
void peek(){
if(vc.size()==0){
cout<< "The stack is empty"<<endl;
}
cout<< vc[length]<<endl; //----> does not work;
//cout<<vc[vc.size()-1]; ---> does not work either
//cout<<vc.end(); ----> does not work either;
}
void add(string value){
vc.push_back(value);
length++;
}
void pop(){
vc.erase(vc.end());
length--;
}
void show(){
for (int i=0;i<vc.size();i++){
cout << vc[i] << " ";
}
cout<<endl;
}
};
int main()
{
Stack mystack;
mystack.peek();
mystack.add("Hello");
mystack.peek();
mystack.add("frands");
mystack.add("chai");
mystack.add("pee");
mystack.add("lo");
mystack.show();
mystack.peek();
mystack.pop();
mystack.show();
}
problem 1->
the problem arises in the peek() function i cannot access the last element in my vector space i get returned with a segmentation fault(core dump) error.
problem 2->
and while pasting this code on stack overflow i had to manually add 4spaces in each code line how to do this at one step (sorry for a silly question).
There are a few problems in your code:
void peek(){
if(vc.size()==0){
cout<< "The stack is empty"<<endl;
}
cout<< vc[length]<<endl; //----> does not work;
}
If vc.size() == 0, you print out a message, and then proceed to index into the empty vector. You should return inside that if, to avoid looking at an invalid index.
Below it you use use a length variable, which I presume is the acting the role of the vector's size(). EITHER you need to ensure the vector's size is the same as the stack's logical size (and then you don't need a length variable), OR you should be testing if length == 0, and not looking at size() here. Otherwise, you could have a positive size of vector, and a length of zero, and what's logically empty may print junk values.
Another serious mistake, your pop function:
void pop(){
vc.erase(vc.end()); // <<< MISTAKE
length--;
}
This is erasing the "end" element, which is not a valid position to erase. Remember, end denotes the first place after the last valid element in the vector, so your code yields undefined behavior. You should use the vector function pop_back instead, as it does precisely what you want. It also will reduce the size of the vector, meaning you don't need a length variable at all! Instead you can use the vector's size().
Next, in peek():
cout<< vc[length]<<endl;
When a vector contains N things, they are indexed from 0..(N-1). Using the number of elements will go too far into the vector. Instead, use length-1 (or vc.size()-1 after fixing pop()). However, you can use vc.back() to access the last element without any need to compute its offset.

push_back iterator in vector?

I tried to store the number 1,4,7...97 into a vector.
if I use std::out << i; the for loop is working.
but the push_back gives an error:
base of member reference is a function perhaps you meant to call it with no arguments.
i googled it and couldn't find a answer which helped me.
The only thing I found was this:
"In C++ adding elements to a vector
may cause reallocation of the contained data,
which will invalidate all iterators. That means
you can't loop over the vector using iterators
(which is what the range-based for loop does)
while also inserting new elements.
You can however iterate using indexes and use
the vector size as condition, since indexes will always be the same."
but do I loop over the vector?
I thought I just store the iterator
I appreciate every help
std::vector<int>rock();
int i;
for (int i = 1; i < 100; i+=3)
{
std::cout<< " " << i;
rock.push_back(i);
}
std::vector<int>rock(); is a .. function declaration.
use std::vector<int>rock; or std::vector<int>rock{}; instead.
The vector declaration
std::vector<int>rock();
is actually a function declaration, that returns a vector.
That's not what you intended. Drop the ():
std::vector<int> rock;

Adding vector to 2D vector, and keeping reference to the last vector

I am writing a program and there was a very subtle error that was hard to recognize.
I examined the program for hours and it looks like it's from my misuse of resize() in allocating a new vector in 2D vector, from my misuse of back() in vector, or from updating a reference variable.
I wrote a simpler code that contains similar problem in my original program for demonstration:
int main() {
vector<vector<int>> A(1);
vector<int> &recent = A.back();
recent.emplace_back(50);
cout << A.back()[0] << endl; //prints 50
A.resize(2);
A[1] = vector<int>();
cout << A.size() << endl; //prints 2
recent = A.back();
cout << A[1].size() << endl; //prints 0
recent.emplace_back(20); //Segmentation fault occurs!!
cout << A[1][0] << endl;
}
Segmentation fault occurs when I tried to emplace_back(20), although in my original program it doesn't throw any error and doesn't emplace the element either.
Possible cause of problem, in my opinion is:
I used resize() to allocate a new vector after the current last position of the 2D vector A, because I didn't know how to emplace_back() an empty vector.
2, 3. In recent = A.back();, I'm not sure if I am updating the reference variable(defined as vector<int> &recent) correctly, and if back() gives the correct reference to the newly allocated vector at the end of the 2D vector A.
The logic looked perfectly fine, but obviously I am doing something wrong.
What am I doing wrong, and what can I do instead?
References in C++ cannot be "updated". The call to resize may (and likely will) invalidate any reference to the original content of the vector. Thus recent is a dangling reference after A.resize(2);.
When creating the initial A here
std::vector<std::vector<int>> A(1);
the outer vector is required to be able to store one single vector.
If you add another std::vector<int> to A the first element of A is likely to move to another memory location. Since recent will always refer to the old memory location you see the segfault.
See 'c++ Vector, what happens whenever it expands/reallocate on stack?' for how vectors work.
On the question how to circumvent this:
If you know the size of the vector in advance you could use reserve to prevent the vector A from reallocating its contents. You'd nevertheless face the problem that references cannot be "reassigned". You can always use A.back() to refer to the last element.
You can use a function taking a reference argument which will be bound upon calling the function:
void do_stuff(std::vector<int> & recent)
{
// do stuff with recent
}
std::vector<std::vector<int>> A;
while (condition)
{
// add whatever to A
A.emplace_back(std::vector<int>{});
// do stuff with last element
do_stuff(A.back());
}
Another way to do it is with scope:
std::vector<std::vector<int>> A(1);
{
std::vector<int> &recent = A.back();
recent.emplace_back(50);
std::cout << A.back()[0] << std::endl; //prints 50
A.resize(2);
} // recent goes out of scope here
std::cout << A.size() << std::endl; //prints 2
{
std::vector<int> &recent = A.back(); // another recent indepedant of first one
std::cout << A[1].size() << std::endl; //prints 0
recent.emplace_back(20);
std::cout << A[1][0] << std::endl; // prints 20
}
Let's step through the code line by line.
vector<vector<int>> A(1);
vector<int> &recent = A.back();
Here we create a vector with one default-constructed vector<int> as its contents. We then bind a reference to the last and only element.
recent.emplace_back(50);
cout << A.back()[0] << endl; //prints 50
We now emplace 50 into the sole vector and print it.
A.resize(2);
Now we resize the vector. If space needs to be reallocated, all iterators, pointers and references to the contents are now invalid.
A[1] = vector<int>();
cout << A.size() << endl; //prints 2
This is fine, as there is enough space in A.
recent = A.back();
BANG
This assignment doesn't rebind recent, it tries to assign A.back() to the referencee. If space was reallocated for A, recent is no longer a valid reference, so we run off into the realm of undefined behaviour.
Quite honestly, using A.back() directly rather than maintaining a reference to it is probably your best bet. If you absolutely want to hold some kind of reference to the end, this is a reasonable use of a non-owning pointer.
From the discussion in the comments, it appears that your original problem was:
vector<vector<int>> very_long_name_that_cannot_be_changed;
and that you want a shorthand notation to access the last element of this:
auto& short_name = very_long_name_that_cannot_be_changed;
short_name.resize(100); // will expand the vector, but not change the reference
short_name.back().emplace_back(20); // presto, quick accesss to the last element.
This is proof against resizing, because the reference just tracks the vector, not its last element.