Linear and Binary search using iterator position [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am writing two functions, the first one does a linear search and is supposed to return the position of the first found value if it is in the vector. The second method is a binary search that is done using equal_range and it is supposed to return the same thing as the linear search. Currently they are checking every value as a match. Any help is appreciated.
int linearSearch( const vector<int>& vec, int z)
{
vector<int>::iterator iter = find(vec.begin(), vec.end(), z);
if (iter != vec.end())
return (iter-vec.begin());
else
return -1;
}
int binarySearch( const vector<int>& vec, int z)
{
pair<vector<int>::const_iterator, vector<int>::const_iterator> bounds;
bounds = equal_range(vec.begin(),vec.end(), z);
if (bounds.first != vec.end())
return (bounds.first -vec.begin());
else
return -1;
}

Your description is unclear, but I'll point out some concerns with the code.
In binarySearch() search, there is
bounds = equal_range(vec.begin(),vec.end(), z);
if (bounds.first != v.end())
return (bounds.first -v.begin());
v is not declared anywhere. If the code compiles then, presumably, it is declared somewhere in code you haven't shown. But it may have no relationship to vec. The behaviour of the code (particularly the return) will be undefined.
A rather fundamental difference between the functions is because std::equal_range() assumes a sorted (or partitioned) range, and std::find() does not.

Related

remove duplicate from a vector of pointers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 days ago.
Improve this question
I have a vector of custom objects and am required to sort it under various criteria(i.e. cannot use set). I also have a comparator to do the sorting, which forces me to use a vector of pointers. This way, the code compiles but it is now failing to remove duplicates.
vector<customObj*> vec;
//code to remove duplicate
vector<customObj*>::iterator it;
it = unique(vec.begin(), vec.end());
vec.resize(distance(vec.begin(), it));
//code to sort a vector
bool Comp(customObj *a, customObj *b) {
if (condition) {
return (a->getValue() < b->getX=Value());
}
}
It seems there is a typo in the comparison function
//code to sort a vector
bool Comp(customObj *a, customObj *b) {
if (condition) {
return (a->getValue() < b->getX=Value());
^^^^^^^^^^^^^^^^
}
}
Also the comparison function returns nothing if the condition does not evaluate to true.
I think you mean
//code to sort a vector
bool Comp(customObj *a, customObj *b) {
return condition && (a->getValue() < b->getValue());
}
In the call of std::unique you need to use a binary predicate like
//code to sort a vector
bool Equal(customObj *a, customObj *b) {
return consdition && (a->getValue() == b->getValue());
}
vector<customObj*>::iterator it;
it = unique(vec.begin(), vec.end(), Equal);
vec.resize(distance(vec.begin(), it));
Pay attention to that if objects in the vector were dynamically allocated then you need to free them. Otherwise you will have memory leaks.
Instead of raw pointers you should use smart pointer std::unique_ptr.

Binary search template for finding a double nearest to the key [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Is there template in C++ , which allows to find in a sorted vector the value nearest to the key, and returns its index (iterator). What I found, returns only a boolean.
Edited: The question was motivated by the fact that the binary search returns a boolean. I conclude that at the time returning the boolean, also the iterator is known. Returning it, I could find out which element is found. What is the reason that no such binary search is provided?.
I don't know if you want to avoid sorting yourc container / array, so I'm going to post most universal way. And the answer is – no. If you don't want to sort container, there's no ready template, but you can simply write your own function, like this:
template<typename iterator_t, typename value_t, typename comparator_t>
iterator_t nearest_element(iterator_t begin, iterator_t end, value_t value, comparator_t comparator) {
return std::min_element(begin, end, [value, &comparator](double lhs, double rhs) {
return comparator(std::abs(lhs - value), std::abs(rhs - value));
});
}
template<typename iterator_t, typename value_t>
iterator_t nearest_element(iterator_t begin, iterator_t end, value_t value) {
return nearest_element(begin, end, value, std::less<value_t>());
}
It's just simple math, nearest element is one with smallest absolute value of subtracting it from searched value. Code above still lets you change comparator (but hides subtraction / std::fabs) so it can be also used to find farthest value.
Usage (without specified comparator) is simple, like any other standard function:
std::vector<double> vec {2., 5., -4.};
auto it = nearest_element(vec.begin(), vec.end(), -10.);
std::cout << *it << std::endl;

Map size different from expected value after adding values to the map [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Basically I have a edgeCostMap of type
map<pair<int, float>, int>
and a vector of 42 vertices. I loop through the vector of vertices and add values to the map as follows:
for(int vertexIndex = 0; vertexIndex < V.size(); vertexIndex++)
{
pair<int, float> toAdd;
toAdd.first = vertexIndex;
toAdd.second = V[vertexIndex].edgeCollapseCost;
edgeCostMap[toAdd] = vertexIndex;
}
However after the loop has been finished and I print out the map contents as follows:
for(map<pair<int, float> ,int>::iterator it = edgeCostMap.begin(); it != edgeCostMap.end(); it++)
{
logFile<<"Vertex "<<it->second<<" has cost "<<it->first.second<<" has "<<mapVF[it->second].size()<<"neighbors"<<endl;
}
I only get back 12 statements. Is my mapping done incorrectly?
Compare function:
class comparator {
public:
bool operator()(const std::pair<int, float>& a, const std::pair<int, float>& b) const {
return a.second < b.second;
}
};
Your comparator considers two keys equal (neither less than the other) if their floats are equal. You cannot put two equal keys in a map. You probably want to use the default comparator but put the float first in the set. This will sort first on the float and second on the integer, ensuring the keys are not equal if their integers differ.

Checking for existence of item in array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there a code that will check if a certain string is inside an array?
For example I have a string "John" then I need to check if "John" is already in the array names[10].
std::find does exactly that:
auto i = std::find(std::begin(a), std::end(a), "John");
if (i != std::end(a)) … // item found!
However, beware that this does a linear pass over the elements. That’s not a problem for 10 items but it becomes inefficient quickly with larger number. For very large sets, use std::unordered_set instead of an array, to attain (theoretically) constant runtime. Alternatively, if you know that the array is sorted you may employ std::binary_search to attain logarithmic runtime.
Here’s the same code, wrapped inside a convenience function:
template <typename It, typename T>
bool contains(It begin, It end, T const& value) {
return std::find(begin, end, value) != end;
}
And here’s a convenience overload for C arrays:
template <typename T, typename U, std::size_t N>
bool contains(T (&arr)[N], U const& value) {
return std::find(arr, arr + N, value) != arr + N;
}
(The syntax for passing arrays to functions is a bit weird.)
If you want to go archaic:
function in_array(String my_array[], String findthis){
len=sizeof my_array;
for(int i=0;i<len;i++){
if(my_array[i]==findthis){
return true;
}
}
return false;
}
then
in_array(array_of_strings, "john");
I wrote this hastily and I'm very tired, but hopefully it can be of some use.

Find all elements in vector and delete them [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have some vector temp = {1,2,2,3,4}. How to find all elem = 2, save there positions in another vector and delete from the first one.
vector<int> another
vector<int>::iterator it;
for (it = temp.begin(); it != temp.end(); )
{
if (it == elem) { //?
another.push_back(it); //?
temp.erase(it++);
}
else
{
++it;
}
}
std::remove(vec.begin(), vec.end(), 2);
this should do the trick.
(Of course, to actually erase them from the vector, you have to write
vec.erase(std::remove(vec.begin(), vec.end(), 2), vec.end());
according to the erase-remove idiom.)
Edit: Just noticed:
save there positions in another vector
("with guns in there hands", right?) - basically that doesn't make a lot of sense, the positions will be invalidated when you remove the elements from the container...
To save the positions:
#include <iterator>
for (auto it = temp.begin(); it != temp.end(); ++it)
if (*it == 2)
another.push_back(std::distance(temp.begin(), it));
To erase:
#include <algorithm>
temp.erase(std::remove(temp.begin(), temp.end(), 2), temp.end());
This works for a larger class of containers than just vectors, thanks to the genericity of iterators.