Remove all duplicate values from a vector? - c++

Input:
1 1 2 2 3
Desired Output:
3
Here is my code:
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <iostream>
using namespace std;
int main(){
vector<int> v;
vector<int>::iterator it;
// input variables
int input, a, arr[10000];
// input
cin >> input;
// comment all your loops, etc
for(int i = 0; i < input ; i++){
cin >> a;
arr[i] = a;
v.push_back(a);
}
for(int j = 0; j < input; j++){
int ch1 = arr[j];
for(int i = 0;i < input; i++){
if(i == j){
}
else{
if(ch1 == arr[i]){
v.erase(std::remove(v.begin(), v.end(), ch1),v.end());
}
else{
}
}
}
}
for(it = v.begin(); it != v.end(); it++){
cout << *it;
}
return 0;
}
erase() is not working here.
How can I solve this problem?

Your problem is that you define two variables with name v.
vector<int>v;
for(int v=0...
So you basically hide your vector with an int and the compiler tries to call erase() for int, which gives you error.
Just change the name of one of these variables.

Related

How to read a 2d triangle array from txt file?

I want to read 2d triangle array from a txt file.
1
8 4
2 6 9
8 5 9 6
I wrote this code. At the end I wanted to print it out if I got the array right. When I run it it does not print the array, but in debug it prints. So there is a problem, but I cannot find it. Sometimes it gives segmentation fault, but I dont understand.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main() {
std::ifstream input_file("input_file.txt");
int size{1};
int **arr = (int**)malloc(3*sizeof(int));
int *lineArr = (int*) malloc(size*sizeof(int));
int temp{};
int index{};
while(input_file >> temp){
lineArr[index] = temp;
index++;
if(index == size){
index = 0;
arr[size-1] = new int[size-1];
for(int i{}; i<size; i++){
arr[size-1][i] = lineArr[i];
}
size++;
lineArr = (int*) realloc(lineArr, size*sizeof(int));
}
}
input_file.close();
for(int a{}; a<size-1; a++){
for(int j{}; j<=a; j++){
std::cout << arr[a][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
You can just use vector instead of malloc. Like this:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
int main() {
ifstream input_file("input_file.txt");
vector<string> numbers;
if (input_file.is_open()) {
string line;
while (getline(input_file, line)) {
numbers.push_back(line);
}
input_file.close();
}
for (vector<string>::iterator t=numbers.begin(); t!=numbers.end(); ++t)
{
cout<<*t<<endl;
}
return 0;
}

Picking about random character without repetition c++

I need to pick m amount of random characters(letters) without repetition and im completely stuck, i keep getting only 1 random letter. How can i fix my code? Is there even a way to fix this or should i just scrap this idea and look for a solution from some kinf od tutorials?
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
cout << "number below 27" << endl;
int m;
cin >> m;
srand(time(NULL));
bool repeat = false;
char letters[m];
char letter;
for(int i = 0; i < m; i++){
letter = rand()%26 +97;
repeat = true;
for(int j = 0; j < m; j++){
if(letters[m] == letters[j]){
repeat = false;
break;
}
}
if(repeat){
letters[m] = letter;
}
}
for (int i = 0; i < m; i++){
cout << letters[m];
}
}
You can use suffle -
#include <random>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
char charSet[]={'a','b','c'};//You can add all the charecters
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(charSet,charSet+3,g);
for(auto c : charSet)
{
std::cout<<c;
}
std::cout<<endl;
return 0;
}
bool repeat = false;
vector<char> letters(m);
char letter;
for(int i = 0; i < m; i++){
do
{
repeat = false;
letter = rand()%26 +97; // generate new random number
for(int j = 0; j<=i; j++) // iterate through the already generated numbers
{
if (letter == letters[j]){ // if the generated number already exists, do the while again
repeat = true;
break;
}
}
} while(repeat);
letters[i] = letter; // assign the unique number
cout << letter;
repeat = false;
}
You repeat the random number generator until you have a unique random number.
And to output your values use i because m is constant and out of bounds:
for (int i = 0; i < m; i++){
cout << letters[i];
}
I think the direct method is to use set in C++. The following solution is done just now utilising set to ensure the unique. Hope it could be helpful.
#include <iostream>
#include <ctime>
#include <set>
#include <random>
using namespace std;
int main()
{
cout << "number below 27" << endl;
int m;
cin >> m;
srand(time(NULL));
set<char> letters_set;
while(letters_set.size() < m){
char c = rand()%26+'a';
letters_set.insert(c);
}
for(auto c: letters_set)
cout<<c<<endl;
}
A more efficient solution which also ensure the equal possibility for each letter.
#include <iostream>
#include <ctime>
#include <set>
#include <random>
using namespace std;
int main()
{
cout << "number below 27" << endl;
int m;
cin >> m;
srand(time(NULL));
vector<int> all_letters(26, 'a');
for(int i = 0; i < 26; ++i) all_letters[i] += i;
vector<char> letters_set;
for(int i = 0; i < m; ++i){
int select = rand()%all_letters.size();
letters_set.push_back(all_letters[select]);
all_letters.erase(all_letters.begin()+select);
}
for(auto c: letters_set)
cout<<c<<endl;
}
There is an obvious error in the logic of your code: when you test for repetition you compare to the beyond the end letter only, instead to all those sampled so far. The correct test would be
for(int i = 0; i < m; i++) {
bool repeating;
char tryletter;
do {
tryletter = rand()%26 +97;
repeating = false;
for(auto j=0; j!=i && !repeating; ++j)
repeating = tryletter == letters[j];
} while(repeating);
letters[i] = tryletter;
}
Though this is not the most efficient way to do what you've been asked to do. A more efficient way would be to start with all 26 letters, pick one at random and remove it from the set, then continue to pick and remove random letters. For example
std::string random_letters_without_repetition(std::size_t m)
{
std::string letters;
std::string all = "abcdefghijklmnopqrstuvwxyz";
assert(m <= all.size());
std::random_device r;
std::default_random_engine rng(r());
while(m--) {
std::uniform_int_distribution<std::size_t> uni{0,all.size()-1};
auto index = uni(rng);
letters += all[index];
all.erase(index);
}
return letters;
}

[C++14]I'm using pair but no output

I was writing a code to sort and output the number by using "pair".
I tried some cases bat there was no output.
How should I rewrite the code?
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
int k, i, n;
cin >> n;
pair<int, int> a[n];
for (i = 0; i < n; i++) {
cin >> k;
a[i].first = -k;
a[i].second = i + 1;
}
sort(a, a + n);
for (i = 0; i++; i < n) {
cout << a[i].second;
}
}
for(i=0;i++;i<n){
You meant to write this as:
for(i=0;i<n;i++){

Unexpected segmentation error

I wrote a really simple program, but it's crashing when I try to write the size of a queue (created with STL). I have no idea why, please help.
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, i, x, cut = 0;
queue<int> que;
vector<int> vec;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> x;
vec.push_back(x);
}
sort(vec.begin(), vec.end());
for (i = 0; i < n; i++)
que.push(vec[i]);
while (!que.empty()) {
cout << que.size() << '\n';
cut += que.front();
while (que.front() <= cut)
que.pop();
}
return 0;
}
You get an error because you call front while the queue is empty.
Just check if the queue is empty in your inner loop:
#include <queue>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, i, x, cut = 0;
queue<int> que;
vector<int> vec;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> x;
vec.push_back(x);
}
sort(vec.begin(), vec.end());
for (i = 0; i < n; i++)
que.push(vec[i]);
while (!que.empty()) {
cout << que.size() << '\n';
cut += que.front();
while (!que.empty() && que.front() <= cut )
que.pop();
}
return 0;
}
Your code is actually crashing on the line:
while (que.front() <= cut)
Because you have a loop that may be true for the whole queue. The next line pops a value. At some point, your queue is empty and que.front() will crash.

c++ output increasing numbers

Hi I have an array of share prices but I only want to output them as they increase.
For example if I have 1,1,1,2,2,2,3,3,3,4,4,4,5,5,5, etc. I only want to print 1,2,3,4.
I have tried setting a temporary max and min but still can't get it.
Now I only have this:
for(int h = 0; h < max; h++)
{
if(v3[h].getPrice() > 0)
{
ofile << v[h].getPrice() << ", ";
}
}
What you want is this
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
// Assign your vector
int a[] = {1,1,1,2,2,3,3,3,4,4,5,5,5,1,3};
vector<int> vec(a, a+15);
// Sort before calling unique
sort(vec.begin(), vec.end());
// Impose only one of each
vector<int>::iterator it;
it = unique(vec.begin(), vec.end());
vec.resize( distance(vec.begin(),it) );
// Output your vector
for( vector<int>::iterator i = vec.begin(); i!= vec.end(); ++i)
cout << (*i) << endl;
return 0;
}
Live example
The sort is necessary for unique to work.
#include <iostream>
using namespace std;
int main()
{
int a[15] = {1,1,1,2,2,2,3,3,3,4,4,4,5,5,5};
for (int i=0; i<15; i+=3)
{
cout << a[i] <<",";
}
return 0;
}
Increment the counter 3 times in the loop " for(int h=0;h < max; h+=3){} ".