The intended nature of the program is to randomly generate numbers into a vector container, then sort them in ascending order and print the numbers out. The program compiles correctly but also sorts and prints the value 0 for each number that is generated.
#include "stdafx.h"
//contains all header files (ctime, algorithm, iostream, vector), also
defines const vector size as 250
using namespace std;
void genRndNums(vector <int> &v);
void printVec(vector <int> &v);
int main()
{
vector <int> myVector(VEC_SIZE);
genRndNums(myVector);
printVec(myVector);
return 0;
}
void genRndNums(vector <int> &v)
{
int v1;
srand(time(nullptr));
for (int i = 0; i < VEC_SIZE; i++)
{
v1 = rand() % 1000 + 1;
v.push_back(v1);
//v.push_back(rand() % 1000 + 1);
}
}
void printVec(vector <int> &v)
{
vector<int>::iterator it;
sort(v.begin(), v.end());
for (it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
}
In the case that 250 numbers were printed out, it would display the integer 0 250 times and then display the rest of the numbers in ascending sequence.
This is due to the for-loop in the print function having something misplaced, but I am not sure how to get the vector to display only the randomized integers.
Since you are using push_back to add elements to the vector, you need to start with an empty vector. I.e. you need to replace vector <int> myVector(VEC_SIZE); with vector <int> myVector;.
This is a common mistake. push_back adds to the end of the vector whose size is already VEC_SIZE. You can start with an empty vector like this:
vector <int> myVector;
or you can assign values to existing vector (whose size is VEC_SIZE) elements like this:
for (int i = 0; i < VEC_SIZE; i++)
{
v1 = rand() % 1000 + 1;
v[i] = v1;
}
Your vector constructor creates vector with 250 numbers (each with 0 value). To that numbers you appends another 250 generated numbers.
You shall not create that 250 numbers at the beginning
#include <vector>
#include <algorithm>
#include <random>
#include <iostream>
int main()
{
std::vector<int> v;
{
std::random_device r;
std::default_random_engine e(r());
std::uniform_int_distribution<int> d(1, 6);
std::generate_n(std::back_inserter(v), 250, [&](){ return d(r); });
}
std::sort(v.begin(), v.end());
for (auto x : v) {
std::cout << x << "\n";
}
return 0;
}
or override them
#include <vector>
#include <algorithm>
#include <random>
#include <iostream>
int main()
{
std::vector<int> v(250);
{
std::random_device r;
std::default_random_engine e(r());
std::uniform_int_distribution<int> d(1, 6);
std::generate(v.begin(), v.end(), [&](){ return d(r); });
}
std::sort(v.begin(), v.end());
for (auto x : v) {
std::cout << x << "\n";
}
return 0;
}
Related
I've searched the Internet and known how to delete an element (with std::erase) and finding duplicates of an element to then delete it (vec.erase(std::unique(vec.begin(), vec.end()),vec.end());). But all methods only delete either an element or its duplicates.
I want to delete both.
For example, using this vector:
std::vector<int> vec = {2,3,1,5,2,2,5,1};
I want output to be:
{3}
My initial idea was:
void removeDuplicatesandElement(std::vector<int> &vec)
{
std::sort(vec.begin(), vec.end());
int passedNumber = 0; //To tell amount of number not deleted (since not duplicated)
for (int i = 0; i != vec.size(); i = passedNumber) //This is not best practice, but I tried
{
if (vec[i] == vec[i+1])
{
int ctr = 1;
for(int j = i+1; j != vec.size(); j++)
{
if (vec[j] == vec[i]) ctr++;
else break;
}
vec.erase(vec.begin()+i, vec.begin()+i+ctr);
}
else passedNumber++;
}
}
And it worked. But this code is redundant and runs at O(n^2), so I'm trying to find other ways to solve the problem (maybe an STL function that I've never heard of, or just improve the code).
Something like this, perhaps:
void removeDuplicatesandElement(std::vector<int> &vec) {
if (vec.size() <= 1) return;
std::sort(vec.begin(), vec.end());
int cur_val = vec.front() - 1;
auto pred = [&](const int& val) {
if (val == cur_val) return true;
cur_val = val;
// Look ahead to the next element to see if it's a duplicate.
return &val != &vec.back() && (&val)[1] == val;
};
vec.erase(std::remove_if(vec.begin(), vec.end(), pred), vec.end());
}
Demo
This relies heavily on the fact that std::vector is guaranteed to have contiguous storage. It won't work with any other container.
You can do it using STL maps as follows:
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
void retainUniqueElements(vector<int> &A){
unordered_map<int, int> Cnt;
for(auto element:A) Cnt[element]++;
A.clear(); //removes all the elements of A
for(auto i:Cnt){
if(i.second == 1){ // that if the element occurs once
A.push_back(i.first); //then add it in our vector
}
}
}
int main() {
vector<int> vec = {2,3,1,5,2,2,5,1};
retainUniqueElements(vec);
for(auto i:vec){
cout << i << " ";
}
cout << "\n";
return 0;
}
Output:
3
Time Complexity of the above approach: O(n)
Space Complexity of the above approach: O(n)
From what you have searched, we can look in the vector for duplicated values, then use the Erase–remove idiom to clean up the vector.
#include <vector>
#include <algorithm>
#include <iostream>
void removeDuplicatesandElement(std::vector<int> &vec)
{
std::sort(vec.begin(), vec.end());
if (vec.size() < 2)
return;
for (int i = 0; i < vec.size() - 1;)
{
// This is for the case we emptied our vector
if (vec.size() < 2)
return;
// This heavily relies on the fact that this vector is sorted
if (vec[i] == vec[i + 1])
vec.erase(std::remove(vec.begin(), vec.end(), (int)vec[i]), vec.end());
else
i += 1;
}
// Since all duplicates are removed, the remaining elements in the vector are unique, thus the size of the vector
// But we are not returning anything or any reference, so I'm just gonna leave this here
// return vec.size()
}
int main()
{
std::vector<int> vec = {2, 3, 1, 5, 2, 2, 5, 1};
removeDuplicatesandElement(vec);
for (auto i : vec)
{
std::cout << i << " ";
}
std::cout << "\n";
return 0;
}
Output: 3
Time complexity: O(n)
Vector function emplace()
My emplace function does not work. Any help would be appreciated
vector <int> vec1;
vector <int> vec2(4,0);
vector <int>::iterator iter1;
vector <int>::iterator iter2;
srand(time(NULL));
for(i=0; i<5; i++){
n =rand()%10+1;
vec1.push_back(n);
}
for(iter1=vec1.begin();iter1<vec1.end();iter1++){
for(iter2=vec2.begin();iter2<vec2.end();iter2++){
if(*iter1<=*iter2){
//vec2.emplace(iter1,*iter1);
//print();
}
}
}
for(iter2=vec2.begin();iter2<vec2.end();iter2++){
Because vec2 starts populated with four values of 0, you will never find an element where *iter1 <= *iter2 unless *iter1 == 0.
Instead of zero-initializing it to avoid allocations, you want to reserve space.
vec2.reserve(vec1.size());
and then instead of a for loop you can use std::lower_bound to find the insert location:
#include <iostream>
#include <vector>
#include <algorithm>
void print_vec(const char* label, const std::vector<int>& vec) {
std::cout << label << ": ";
for (int v : vec) {
std::cout << v << ", ";
}
std::cout << "\n";
}
int main() {
std::vector <int> vec1 { 4, 1, 2, 2, 5 };
std::vector <int> vec2;
print_vec("v1", vec1);
vec2.reserve(vec1.size());
for (int v : vec1) {
auto it = std::lower_bound(vec2.begin(), vec2.end(), v);
if (it == vec2.end() || *it != v)
vec2.emplace(it, v);
print_vec("v2", vec2);
}
print_vec("Fin", vec2);
return 0;
}
Live demo: http://ideone.com/o5td9K
What you are trying to do is like insertion sort.
https://en.wikipedia.org/wiki/Insertion_sort
has the pseudo-code. At each step you will check every element in vector 2 and place the new element where it belongs (while loop).
I'm beginner in programming. Something is giving me trouble to code. Suppose, I've an array.
int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
I want to remove all elements which are greater than 9. How can I do this?
You can do this if you use vector. First initialize vector with your array. Then use remove_if() function. Hope this will help.
#include <algorithm>
#include <vector>
int main()
{
int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
vector<int> V(Array, Array+20);
vector<int> :: iterator it;
it = remove_if(V.begin(), V.end(), bind2nd(greater<int>(), 9));
V.erase (it, V.end()); // This is your required vector if you wish to use vector
}
You cannot remove items from an array, since they are fixed in size.
If you used std::vector, then the solution would look like this:
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
std::vector<int> Array = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
Array.erase(remove_if(Array.begin(), Array.end(), [](int n) { return n > 9; }),
Array.end());
copy(Array.begin(), Array.end(), ostream_iterator<int>(cout, " "));
}
Live example: http://ideone.com/UjdJ5h
If you want to stick with your array, but mark the items that are greater than 10, you can use the same algorithm std::remove_if.
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
int *overwrite_start = remove_if(std::begin(Array), std::end(Array), [](int n){ return n>9; });
fill(overwrite_start, std::end(Array), -1);
copy(std::begin(Array), std::end(Array), ostream_iterator<int>(cout, " "));
}
The above will move the "erased" items to the end of the array, and mark them with -1.
Live example: http://ideone.com/7rwaXy
Note the usage in both examples of the STL algorithm functions. The second example with the array uses the same remove_if algorithm function. The remove_if returns the start of the "erased" data, as remove_if doesn't actually remove, but moves the data to the end of the sequence.
i am try swap concept without using vector
int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
int n;
int arr_len = sizeof(Array)/sizeof(int);
void print_array_value() {
int i;
cout << "\n";
for (i = 0; i < arr_len; i++) {
cout << Array[i] << ", ";
}
cout << " : " << arr_len << "\n";
}
void swap_array_value(int start) {
int i;
for ( ; (start+1) < arr_len; start++) {
Array[start] = Array[start+1];
}
}
void remove_array_value() {
int i;
for (i = 0; i < arr_len; i++) {
if (Array[i] > n) {
swap_array_value(i);
arr_len--;
i--;
}
}
}
void main () {
clrscr();
cout << "Enter the N value : ";
cin >> n;
print_array_value();
remove_array_value();
print_array_value();
cout << "Array Length : " << arr_len;
getch();
}
I have a hand full of objects each containing several strings each. Right now its set up as structures each containing a map with keys 1...n, one for each string (map<int,string> strs), this can be changed if a better way exists. I need to randomly access all of these str ings without overlapping and know that i'm done. How can I do this, either with the maps or another data structure? Thanks.
Here's some code for a Fisher-Yates shuffle:
template <class T>
std::vector<T> shuffle(std::vector<T> &vect)
{
std::vector<T> shuffled = vect;
for(int i = shuffled.size()-1; i >= 1; i--) {
int idx = rand() % (i+1);
T tmp = shuffled[idx];
shuffled[idx] = shuffled[i];
shuffled[i] = tmp;
}
return shuffled;
}
This will take in a vector, and return a copy of it in a random order. If you have a vector of strings, you can use it like this (I'm using c++11 here):
int main()
{
srand(time(NULL));
std::vector<std::string> strs = {"foo", "bar", "baz", "stack", "overflow"};
for(auto &str : shuffle(strs)) {
std::cout << str << std::endl;
}
return 0;
}
Of course, if you're lazy, like me, there's always the random_shuffle() function in <algorithm>:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main()
{
std::vector<std::string> strs = {"foo", "bar", "baz", "stack", "overflow"};
std::random_device rd;
std::mt19937 g(rd()); // Use a good random number generaor
std::random_shuffle(strs.begin(), strs.end(), g); // this does the shuffle
for(auto &str : strs) {
std::cout << str << std::endl;
}
return 0;
}
Hope this helps!
A horrible solution, don't do this. Very slow for large candidate vectors, this has n-squared complexity. Shuffling is better, it has linear complexity.
std::vector<int> RandomThing(int number, int min, int max)
{
assert(!"RandomThing" && min < max);
std::vector<int> candidates;
for(int i=min; i<max; i++)
candidates.push_back(i);
std::vector<int> result;
for(int i=0; i<number;)
{
int candidate_index = rand() % candidates.size();
result.push_back(candidates[candidate_index]);
std::vector<int>::iterator it = candidates.begin();
std::advance(it, candidate_index);
candidates.erase(it);
}
return result;
}
Say I have a vector with values [1,2,3,4,5,6,7,8,9,10]. I want to create a new vector that refers to, for example, [5,6,7,8]. I imagine this is just a matter of creating a vector with pointers or do I have to push_back all the intermediary values I need?
One of std::vector's constructor accepts a range:
std::vector<int> v;
// Populate v.
for (int i = 1; i <= 10; i++) v.push_back(i);
// Construct v1 from subrange in v.
std::vector<int> v1(v.begin() + 4, v.end() - 2);
This is fairly easy to do with std::valarray instead of a vector:
#include <valarray>
#include <iostream>
#include <iterator>
#include <algorithm>
int main() {
const std::valarray<int> arr={0,1,2,3,4,5,6,7,8,9,10};
const std::valarray<int>& slice = arr[std::slice(5, // start pos
4, // size
1 // stride
)];
}
Which takes a "slice" of the valarray, more generically than a vector.
For a vector you can do it with the constructor that takes two iterators though:
const std::vector<int> arr={0,1,2,3,4,5,6,7,8,9,10};
std::vector<int> slice(arr.begin()+5, arr.begin()+9);
You don't have to use push_back if you don't want to, you can use std::copy:
std::vector<int> subvector;
copy ( v1.begin() + 4, v1.begin() + 8, std::back_inserter(subvector) );
I would do the following:
#include <vector>
#include <iostream>
using namespace std;
void printvec(vector<int>& v){
for(int i = 0;i < v.size();i++){
cout << v[i] << " ";
}
cout << endl;
}
int main(){
vector<int> v;
for(int i = 1;i <= 10;i++) v.push_back(i);
printvec(v);
vector<int> v2(v.begin()+4, v.end()-2);
printvec(v2);
return 0;
}
~