erase entire C++ vector [duplicate] - c++

This question already has answers here:
Delete all items from a c++ std::vector
(9 answers)
Closed 2 years ago.
I would like to know which of the following version is the more appropriate to empty a vector, or if there is even a better way:
std::vector<T> v;
// 1
v.erase(v.begin(), v.end());
// 2
v = {};
My thought is that one has a complexity greater than the other, but one saves memory reallocation...

None. The idiomatic way is to call clear();:
std::vector<T> v = { ... };
v.clear();

Related

Other than coding the whole method, can I use an in-built function in C++ to find the most occurring element in an array? [duplicate]

This question already has answers here:
Find which numbers appears most in a vector
(5 answers)
Closed 11 months ago.
I have an array v = [5,1,5,4,5,2,3,4,5,6,5]. I want to return the most frequently occurring value, which is 5 in this case. Is there an in-built function in C++ for the same?
I can implement a method to calculate the frequency of each element thereby returning the value with maximum frequency but I would very much like to know if there is a better way to do the same.
You can use std::unordered_map container with std::max_element function to find most frequent element in an array or vector. For instance:
int getMostFrequentElement(vector<int> &arr)
{
if (arr.empty())
return -1;
unordered_map<int, int> freq_count;
for (const auto &item : arr)
freq_count[item]++;
auto most_freq_int =
std::max_element(freq_count.begin(), freq_count.end(),
[] (const auto &x, const auto &y) {return x.second < y.second;});
return most_freq_int->first;
}

Runtime exception when using vector.insert() [duplicate]

This question already has answers here:
Vector iterator erase giving me a runtime error?
(2 answers)
Replacing elements in vector using erase and insert
(3 answers)
Erasing from a std::vector while doing a for each?
(1 answer)
Closed 1 year ago.
So I have the following code, which is supposed to use an iterator to replace an element in a vector if it matches some given value "old".
using StringIter = std::vector<std::string>;
using StringVec = std::vector<std::string>;
void replace(StringVec ref, std::string old, std::string replacement)
{
for (StringIter::iterator it = ref.begin(); it != ref.end(); it++)
{
if (*it == old)
{
ref.erase(it);
ref.insert(it,replacement);
}
}
}
However, I get the following runtime error: "vector emplace iterator outside range". I've tried changing the order of erase and insert, and using insert(it-1,...) instead, but it doesn't work. Haven't found any solution to this exact problem after an hour or so of googling.

Range Based For Loop in C++ : How it is working? [duplicate]

This question already has answers here:
How to make my custom type to work with "range-based for loops"?
(10 answers)
How to correctly implement custom iterators and const_iterators?
(9 answers)
Closed 2 years ago.
std::vector<int> v = {0, 1, 2, 3, 4, 5};
for (auto i : v)
{
// access by value, the type of i is int
std::cout << i << ' ';
}
std::cout << '\n';
I found this as part of another question
How to navigate through a vector using iterators? (C++)
I would like to know how the following works ?
for (auto i : v)
will navigate the entire vector, I need this information because I want to implement a custom vector class. In order to create custom vector class, do I need to create the begin(), end() functions and iterator member variable in it?

Delete elements from a vector in C++11 while iterating over it [duplicate]

This question already has answers here:
Erasing elements from a vector
(6 answers)
Closed 6 years ago.
My applications requires to iterate over a vector and delete certain elements which doesnt satisfy the requirements. Which is the most correct way? I believe the following way is incorrect.Reason: I am getting segmentation fault.
std::vector<ObjectX> vec1;
//Fill in vec1
std::vector<ObjectX>::iterator itVec1 = vec1.begin();
for(;itVec1 != vec1.end(); ++itVec1) {
if (Oracle(*itVec1)) vec1.erase(itVec1);
}
When you call
vec1.erase(itVec1);
you invalidate itVec1. After that, ++itVec1 is not right. It leads to undefined behavior. You need to change your code a little bit.
for( ; itVec1 != vec1.end(); ) {
if (Oracle(*itVec1))
{
itVec1 = vec1.erase(itVec1);
}
else
{
++itVec1;
}
}
You can remove all the boiler plate code by using the Erase-Remove Idiom:
vec1.erase(std::remove_if(vec1.begin(), vec1.end(), Oracle), vec1.end());
You could just put the correct items into a new vector.

go through map c++ [duplicate]

This question already has answers here:
How can I loop through a C++ map of maps?
(9 answers)
Closed 9 years ago.
So, I have an std::map<int, my_vector> and I want to go through each int and analyse the vector.
I haven't gotten to the part of analyzing the vector just yet, I'm still trying to figure out how to go through every single element on the map.
I know it is possible to have an iterator, but I didn't quite understood how it works, also, I don't know if there isn't a better way to do what I intend to do
You can simply iterate over the map. Each map element is an std::pair<key, mapped_type>, so first gives you the key, second the element.
std::map<int, my_vector> m = ....;
for (std::map<int, my_vector>::const_iterator it = m.begin(); it != m.end(); ++it)
{
//it-->first gives you the key (int)
//it->second gives you the mapped element (vector)
}
// C++11 range based for loop
for (const auto& elem : m)
{
//elem.first gives you the key (int)
//elem.second gives you the mapped element (vector)
}
Iterators are the perfect thing for this. Look around http://www.cplusplus.com/reference/map/map/begin/