Input vector without temporary variable [duplicate] - c++

This question already has answers here:
Getting input directly into a vector in C++
(5 answers)
Closed 6 years ago.
How do I input vector without a temporary variable(like x in this example)?
std::vector<int> a;
int n, x;
std::cin >> n;
for (int i=0;i<n;i++)
{
std::cin >> x;
a.push_back(x);
}

One possible solution:
int n, x;
std::cin >> n;
std::vector<int> a(n);
for (int i=0;i<n;i++){
std::cin >> a[i];
}

Related

Getting SIGESV error in following cpp code [duplicate]

This question already has answers here:
Segmentation Fault (SIGSEGV) when getting data as input in the vector of struct
(2 answers)
Closed 2 years ago.
Getting SIGESV error in following cpp code, kindly check.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
int sa = 0, sb = 0, c = 0;
cin >> n >> m;
vector<int> a;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b;
for (int i = 0; i < m; i++) {
cin >> b[i];
}
int k = min(n, m);
while (k--) {
sa = accumulate(a.begin(), a.end(), 0);
sb = accumulate(b.begin(), b.end(), 0);
if (sb >= sa) {
swap(*min_element(a.begin(), a.end()),
*max_element(b.begin(), b.end()));
c++;
} else {
break;
}
}
if (sb >= sa) {
cout << "-1" << endl;
} else {
cout << c << endl;
}
}
return 0;
}
The code asks for two arrays and swaps only if array a has a summation less than array b.
Getting error as SIGESV, also can take 'b' array as an input, what to do?
When you're doing cin >> a[i], a is still empty, so attempting to access its i-th element produces a segfault. Resize it before with a.resize(n) or initialise it with the proper size: vector<int> a(n);
The size and capacity of vector a is 0 and never changes.
This code is broken:
vector<int> a;
for(int i=0;i<n;i++){
cin>>a[i];
}
operator[]() does not grow the vector. You are assigning values to memory that does not belong to a.
You want a.push_back(i) at a minimum. What you probably really want is:
vector<int> a;
a.reserve(n);
for (int i = 0; i < n; ++i) {
int tmp;
cin >> tmp;
a.push_back(tmp);
}
Reserving memory stops the vector from re-allocating. Not doing it in a constructor also stops default initialization of all elements.
The same goes for the b vector.
You also never change the values of sa or sb, but that shouldn't crash anything, it's just a logic error from what I can see.
The file as a whole is also rife with bad practices. "Competitive" coding sites actively make you a worse programmer.
When you know size you need while declaring vector you should declare vector like this
vector<int>arr(n);
vector<int>arr2(m);
this will create an vector of size n will value being zero at all indexes in vector at initialization.
Don't use <bits/stdc++.h> and trying coming up for better name for variables it will really help you as well as us. I know this is compeitive programming question but still.

How can i solve my constant error in visual studio? [duplicate]

This question already has answers here:
Variable Length Array (VLA) in C++ compilers
(2 answers)
Closed 2 years ago.
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int m[n][2];
for (int i = 0; i < n; i++) {
cin >> m[i][0];
cin >> m[i][1];
}
}
if you run this codes in visual studio, then i get errors at line 9.
Can anyone tell why i have errors?
It looks like you're trying to initialize m as a 2d array with the first dimension's size as user input, and the second as constant 2. To do that you can use
int n;
cin >> n;
int (*m)[2] = new int[n][2];
for (int i = 0; i < n; i++) {
cin >> m[i][0];
cin >> m[i][1];
}

How to find Max and its index in vector in c++? [duplicate]

This question already has answers here:
Finding the position of the maximum element
(6 answers)
The Definitive C++ Book Guide and List
(1 answer)
Closed 2 years ago.
Please have a look at the following code. I've tried using max_element function and find function to complete the task, but shows segmentation error, when compiled. I've checked it is not out of bound. Kindly help me out, Also, if possible kindly suggest me some resource on C++ I know badics, I'm having difficulty in the STL part.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
double bag( int w, vector<int> values,vector<int> weights){
int index, ma;
double W1=0.0;
double V=0.0;
while(W1<=w)
{
ma=*max_element(values.begin(), values.end()) ;
auto it = find(values.begin(), values.end(), ma);
index = distance(values.begin(),it);
if(w-W1>=weights[index])
{
V=V+values[index];
W1=W1+weights[index];
}
else
{
V=V+values[index]*(w-W1)/weights[index];
}
values.erase(it);
weights.erase(it);
}
return V;
}
int main() {
int n;
int w;
double ans;
std::cin >> n >> w;
vector<int> values(n);
vector<int> weights(n);
for (int i = 0; i < n; i++) {
std::cin >> values[i] >> weights[i];
}
cout<<values[n-1];
ans = bag(w,values,weights);
cout<<ans;
}
You are "dereferencing" what is returning from max_element without checking if it is values.end(), which must not be "dereferenced".
The loop condition while(W1<=w) should be while(!values.empty() && W1<=w).
Also weights.erase(it); is invalid because it is an iterator for values, not weights.
values.erase(it);
weights.erase(it);
should be
weights.erase(std::next(weights.begin(), std::distance(values.begin(), it)));
values.erase(it);

Get a line of space-separated integers from stdio without knowing how much they are (C++)

I'm dealing with a competitive programming challenge in which I have to take a line of space-separated integers from standard input, put them into an array, and treat them in a certain way. The problem is that I don't know how many integer I may get in each test case.
In case I know, my code would be like:
int n; // number of integers;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
In case I don't have 'n', how would I achieve the same thing?
std::vector<int> is basically a dynamically-sized array of ints. You can keep adding stuff to it and it will grow as necessary. If you are given a number of elements as the first input, you can do something like:
std::vector<int> items;
int count;
std::cin >> count;
// Preallocates room for the items. This is not necessary, it's just an optimization.
items.reserve(count);
while (count > 0) {
int item;
std::cin >> item;
items.push_back(item);
--count;
}
If you are not given the number of items, just read until reading fails:
std::vector<int> items;
int item;
while (std::cin >> item) {
items.push_back(item);
}
Use vectors beacuse vectors are dynamic in size. Keep pushing elements into vector until inputs are there.
std::vector<int> v;
int temp;
while (std::cin >> temp) {
v.push_back(temp);
}
When you will be given the value of n. You can follow any of the following two steps:
Step: 1
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
cin >> n; // Input n
vector<int>vv(n); // It will declare a vector(similar to an array) of size n
for(int i = 0; i < n; i++)
{
cin >> vv[i];
}
return 0;
}
Step: 2
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n, number;
cin >> n; // Input n
vector<int>vv; // It will declare an empty vector
for(int i = 0; i < n; i++)
{
cin >> number; // Take a number as input
vv.push_back(number); // Put the input to the last of the vector
}
return 0;
}
When you will not be given the value of n:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int number;
vector<int>vv; // It will declare an empty vector.
while(cin >> number)
{
vv.push_back(number); // Push(put) the input to the back(end/last) of the vector
}
/* In case of reading input from a file,
the loop will continue until the end of the file.
When you'll try it from console, you need to enter
end-of-file command from keyboard.*/
return 0;
}

In this code, it is not taking input for i=0. It is directly going for i=1 [duplicate]

This question already has answers here:
cin and getline skipping input [duplicate]
(4 answers)
Closed 4 years ago.
in this code it is taking not taking input for i=0. It is taking input directly for i=1.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<string> x;
string q;
for(int i=0;i<n;i++)
{
getline(cin,q);
x.push_back(q);
cout<<x.size();
}
for(int i=0;i<x.size();i++)
cout<<x[i]<<endl;
return 0;
}
Add an extra "getline(cin,q);" statement before "for loop" so that it reads the pending line where you entered input for "cin>>n;" statement.
Your new code will be:-
using namespace std;
int main()
{
int n;
cin>>n;
vector<string> x;
string q;
getline(cin,q); // to read the pending line
for(int i=0;i<n;i++)
{
getline(cin,q);
x.push_back(q);
cout<<x.size();
}
for(int i=0;i<x.size();i++)
cout<<x[i]<<endl;
return 0;
}