#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{ vector <int> a,b;
int i,n,m,k;
bool cond=true;
cin>>n;
for(i=0;i<n;i++)
{
cin>>m;
a.push_back(m);
}
for(i=1;i<n-1;i++)
{
if(a.at(i)==a.at(i+1))
a.erase(a.begin()+i);
}
n=a.size();
for(i=0;i<n;i++)
cout<<a[i];
}
return 0;
}
I want to delete all repetitions of consecutive numbers.
This program throws an "out of bounds" error.
Is there any logical error, or could you suggest some better algorithm?
You are accessing out of bounds since after erasing an element your container is now one element shorter than the number of times you are iterating.
In any case; you can solve your problem in two lines if you just use what's already available in the standard library:
auto last = std::unique(a.begin(), a.end());
a.erase(last, a.end());
See also: http://en.cppreference.com/w/cpp/algorithm/unique
Related
Ive tried to use an 'odd' sorting algorithm just to see if i can manage make it work, basically the algorithm finds the smallest number and places it first, then it repeats this loop but starting from the second number, ignoring the already sorted one completely, but for some reason it seems to not ignore the first sorted number and constantly swaps it out.
#include<iostream>
using namespace std;
int main()
{
int n,i,j;
int min;
int a[10];
cin>>n;
for(i=0;i<n;i++)
{
cout<<"a["<< i <<"]=";
cin>>a[i];
}
min=a[0];
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(min>a[j])
min=a[j];
}
a[j]=a[i];
a[i]=min;
min=100;
}
for(i=0;i<n;i++)
cout<<a[i];
return 0;
}
Why vector.push_back(s) does not work here but vector[i]=s works (mentioned as comment)? If I use the push_back function, nothing gets printed. vec[i]=s works perfectly fine, but why is the push_back not working? I expect both the alternatives to run equally good that is print the stored elements in a 2-d vector.
#include<iostream>
#include<vector>
#include<utility>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int n;
cin>>n;
vector<vector<pair<int,int>>> vec(n);
for(int i=0;i<n;i++)
{
int m;
cin>>m;
vector<pair<int,int>> s;
for(int j=0;j<m;j++)
{
int x;
cin>>x;
s.push_back(make_pair(x,i+1));
}
vec.push_back(s);//vec[i]=s works here :-(
}
for(int i=0;i<n;i++)
for(int j=0;j<vec[i].size();j++)
cout<<vec[i][j].first<<" ";
return 0;
}
You create a vector with n elements here:
vector<vector<pair<int,int>>> vec(n);
If you push n more elements to that vector then it has 2*n elements where the first n elements are still the ones that were present after construction and the last n elements are the ones you pushed.
You just discovered one of the big advantages of not "manually" writing loops. You would have seen what is going on if you had written
for(const auto& row : vec) {
for(cons auto& entry : row) {
std::cout<<entry.first<<" ";
}
}
In general keeping track of the size of a container separately from the the container is a very bad idea. Use .size() if you need to know its size.
Last but not least, using a debugger you could have observed what is going on in your code.
only half of the test cases pass when i traverse through unordered_map, but when i use vector all of them pass, is there any mistake in my code?
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n; cin>>n;
unordered_map<int,int> mp;
vector<int> v; v.reserve(n);
while(n--)
{
int x,y; cin>>x>>y;
int h=y+x;
if(mp[h]==0){ v.push_back(h); }
mp[h]++;
}
int s; cin>>s; int sa=0;
for(auto i:mp)
{
int j=i.first+s;
long long u=mp[j]*(i.second);
sa=sa+u;
}
cout<<sa;
}
In this loop:
for(auto i:mp)
{
int j=i.first+s;
long long u=mp[j]*(i.second);
sa=sa+u;
}
you are potentially modifying mp when you do mp[j], in the case that j is not a key in the unordered_map. Modifying a range that you are iterating over in a range-for loop invokes undefined behavior.
You can use find to check if the key j exists and do something else in that case (but not modify the unordered_map).
I am writing a function in c++ which takes a vector array as a parameter and then finds the number of occurrences of the largest value in it.
I have written function birthdaycakecandles and main function as below.
#include <iostream>
using namespace std;
int birthdaycakecandles(vector<int> a)
{
int largest=a[0];
int pos;
for(int i=1;i<a.size();++i)
{
if(largest<a[i])
{
largest=a[i];
pos=i;
}
}
int count=1;
for(int i=0;i<a.size();++i)
{
if(a[i]==largest&&i!=pos)
count++;
}
return count;
}
int main()
{
double n;
cin>>n;
vector<int> a;
for(double i=0;i<n;++i)
cin>>a[i];
int val=birthdaycakecandles(a);
cout<<val;
return 0;
}
Please review the code and suggest me the reason(s) for the error.
The compiler is giving me segmentation error.
Thanks.
Given the code
vector<int> a;
for(double i=0;i<n;++i)
cin>>a[i];
a is empty; access to nonexistent elements as a[i] leads to UB, anything is possible.
You can initialize a containing n elements from the beginning.
size_t n;
cin>>n;
vector<int> a(n);
for(size_t i=0;i<n;++i)
cin>>a[i];
Or use push_back to insert elements into a.
size_t n;
cin>>n;
vector<int> a;
for(size_t i=0;i<n;++i) {
int x;
cin>>x;
a.push_back(x);
}
PS: Using double as the index of vector seems meaningless.
When using vector, we have to add elements using push_back or emplace_back. In order to traverse the vector, make use of vector iterators for exception safety due to out of bound access.
for (vector<int>::iterator it = a.begin() ; it != a.end(); ++it)
{
//your implementation goes here.
cout << *it << endl; //access each element like this
}
Also note that a.end() is in fact the last_element + 1
vector<int> a;
for(double i=0;i<n;++i)
cin>>a[i];
This is not the way to input in vector use a.push_back(i); instead;
and use auto to traverse till a.begin() till a.end()
I am trying to use STl sort on a vector which is a part of a 2d array, but getting an error. What is the correct way to do this? Trying to use vector normally as sort(A,A+A.size()) where A is the vector.
I have added a comment at the sort statement that is giving an error.
#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
while(1)
{
int m,n;
cin>>m>>n;
int arr[m][n];
for(int x=0;x<m;x++)
for(int y=0;y<n;y++)
cin>>arr[x][y];
vector<int> cost[m][n]; //THIS IS THE 2-D ARRAY OF VECTORS
cost[0][0].push_back(arr[0][0]);
for(int i=1;i<m;i++)
{
cost[0][i].push_back(cost[0][i-1][0]+arr[0][i]);
}
for(int i=1;i<n;i++)
{
cost[i][0].push_back(cost[i-1][0][0]+arr[i][0]);
}
for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
vector<int>::iterator it;
for(it=cost[i-1][j].begin(); it!=cost[i-1][j].end();it++)
cost[i][j].push_back(((*it)+arr[i][j]));
vector<int>::iterator it1;
for(it1=cost[i][j-1].begin(); it1!=cost[i][j-1].end();it1++)
cost[i][j].push_back(((*it1)+arr[i][j]));
//freevec(cost[i][j]);
}
}
int tx,ty,k;
cin>>tx>>ty>>k;
//THIS STEP IS GIVING AN ERROR.
sort(cost[tx][ty],cost[tx][ty]+cost[tx][ty].size());
//THIS STEP IS GIVING AN ERROR.
cout<<cost[tx][ty][k-1]<<endl;
/*
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{ cout<<"\n\n"<<arr[i][j]<<endl;
vector<int>::iterator it;
for(it=cost[i][j].begin(); it!=cost[i][j].end();it++)
cout<<(*it)<<" ";
}
cout<<endl;
}*/
}
}
A vector doesn't change its properties by being an element of an array. So you would sort it just like you would sort a vector that is not an element of a 2D array:
sort(cost[tx][ty].begin(), cost[tx][ty].end());
or
sort(begin(cost[tx][ty]), end(cost[tx][ty]));
Assuming argument dependent lookup finds std::sort, std::begin and std::end.