I want to store values into set without using extra variable.
int n,x;
set<int> s;
cin>>n;
for(int i=0;i<n;i++)
{
cin >> x;
s.insert(x);
}
This is what i have been doing till now, was wondering if i could remove x, as this variable is useless.
I am new to C++. Forgive me if my doubt is very naive.
was wondering if i could remove x
Sure.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
int main() {
std::set<int> s;
int n;
std::cin >> n;
std::copy_n(std::istream_iterator<int>{std::cin}, n,
std::inserter(s, s.begin()));
}
Related
When I try to input to vector by following code, I get a segmentation fault as shown below.
I defined the vector and set N, and tried to input a vector with N elements.
What is the root cause of this?
#include <iostream>
#include <vector>
using namespace std;
int N;
vector<int> A(N);
int main(){
cin>>N;
for(int i=0;i<N;i++) cin>>A[i];
}
[ec2-user#ip-10-0-1-187 ]$ ./vector_input
1
1
Segmentation fault
That's because constructor of A is called with N=0. When you input N, it doesn't affect the vector at all. If you really want to keep using globals, then use std::vector::resize but the recommended way would be:
int main(){
std::size_t n;
std::cin >> n;
std::vector<int> A(n);
for(auto & x : A) std::cin >> x;
}
This happened because N was never initialized and it was used for initiating vector. So, your code produces undefined behavior.
You need to take input first, and then initialize the vector.
Also, avoid using using namespace std;. And there's no real need to initialize A and N in a global scope.
NOTE: To avoid undefined behavior further you enable this flag while compiling C++ programs.
-fsanitize=undefined,address
Code:
#include <vector>
#include <iostream>
int main(void)
{
int N;
std::cin >> N;
std::vector<int> A(N);
for(auto &i : A)
std::cin >> i;
return 0;
}
The variable N used for initialization is not initialized
int main(){
int n,a;
vector<int> vec;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a;
vec.push_back(a);
}
}
this code works well for all samples, but after all samples are finished, a problem occurs. I don’t know what happens and the program crashes. Is there a problem with this code?
i have this problem when i use strings arrays usualy can it be the problem?
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
long long t,n;
int a[1000];
string str[1000];
int main()
{
cin>>t;
for(int r=1;r<=t;r++){
cin>>n;
int maxi=0;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]>maxi)maxi=a[i];
};
//input first value
maxi=maxi+3;
for(int r1=0;r1<maxi;r1++){
str[1][r1]=(rand()%26)+'a';
}
for(int i=0;i<maxi;i++){
cout<<str[1][i];
}
cout<<endl;
//
for(int k=2;k<=(n+1);k++){
int w;
for(w=0 ; w<=a[k-1];w++){
str[k][w]=str[k-1][w];
};
for(int l=w-1;l<maxi;l++){
str[k][l]=(rand()%26)+'a';
};
for(int i=0;i<maxi;i++){
cout<<str[k][i];
}
cout<<endl;
}
}
return 0;
}
You are using elements of strings without allocating them.
Allocate elements by inserting
for(int i=1;i<=(n+1);i++){
str[i].resize(maxi);
}
just after
maxi=maxi+3;
#include <iostream>
using namespace std;
int n, a, k;
main(){
cin>>n;
for(k=1; k<=n; k++){
cin>>a;}}
how can I store inputs to use them in future operations, I'm beginner programmer, I'm looking for easiest way for that.
If you want to read n integers you can use a vector:
#include <iostream>
#include <vector>
int main()
{
int n;
std::vector<int> vec; //Better try to use namespaces
//Some spaces add clarity
std::cin >> n;
vec.resize(n);
for(int k=0; k<n; k++) //Define local varibales relative to loop for iterator
{
std::cin >> vec[k];
}
return 0; //Always return a code at the finish of the program
}
I am making a program, in which I need to iterate the numbers, starting from 1 to num then put the value of that array {1...num} to a variable that can be called in the for loop.
This is where I am at.
int main()
{
int num = 0;
cin >> num;
for (int i = 1; i <= num; i++)
{
procnum[i];
}
}
I need procnum[num] to have a value like ...
int procnum[num] = {1,2,3...num};
If you can use std::vector and std::iota, this is just two line code.
No need to for(index) loop the array. See live example here
#include <vector> // std::vector
#include <numeric> // std::iota
std::vector<int> procnum(some_size);
std::iota(procnum.begin(), procnum.end(), 1);
In C++11, no need to even write a loop, unless you need to do error checking (e.g. check that reading input succeeded, or that the input value is valid).
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
int main()
{
int size;
std::cin >> size;
std::vector<int> procnum(size);
std::iota(procnum.begin(), procnum.end(), 1); // starting value is 1
// output vector to demonstrate it is populated
std::copy(procnum.begin(), procnum.end(), std::ostream_iterator<int>(std::cout," ");
std::cout << "\n";
}
Before C++11, there was no algorithm std::iota(), but it is possible to use std::generate() and a simple functor to achieve the same effect.
you can use std::vector to create dynamic arrays:
#include <iostream>
#include <vector>
int main() {
int size;
std::cin >> size;
std::vector<int> procnum(size);
for (int i = 0; i < size; ++i) {
procnum[i] = i + 1;
}
}
you shouldn't use namespace std; - read here why.
Given the following program:
#include <vector>
#include <iostream>
using namespace std;
int main()
{
unsigned n;
vector<double> xvec;
cin >> n;
while (xvec.size() < n)
{
double x;
cin >> x;
xvec.push_back(x);
}
return 0;
}
Is there a way, using STL, to write this without the explicit while loop (e.g., using a copy() algorithm and an inserter?).
I haven't found a way to do this when the number of elements is read at runtime (as in the variable 'n', here).
Here is one way to copy n doubles into a vector:
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
unsigned n;
std::vector<double> xvec;
std::cin >> n;
xvec.reserve(n);
std:generate_n(std::back_inserter(xvec), n,
[]() { double d; std::cin >> d; return d; });
std::copy(xvec.begin(), xvec.end(),
std::ostream_iterator<double>(std::cout, " "));
std::cout << "\n";
return 0;
}
Note: This alternative using std::copy_n won't work:
std::copy_n(std::istream_iterator<double>(std::cin),
n,
std::back_inserter(xvec));
This actually reads n+1 elements but only copies n of them (at least on g++ 4.6.3).
Note 2: Limiting the answer to C++98, here is one possible solution:
double GetD() { double d; std::cin >> d; return d; }
...
std:generate_n(std::back_inserter(xvec), n, GetD);