Hello All
is there anyway I can find two elements in a vector in one iteration? e-g
Code:if(find(this) AND find(this))
{
// then do this
}
Thanks
Printable View
Hello All
is there anyway I can find two elements in a vector in one iteration? e-g
Code:if(find(this) AND find(this))
{
// then do this
}
Thanks
Basic approach:
Edit:Code:bool find( const std::vector<T>& v, const U& crit1, const U& crit2 )
{
bool found1 = false;
bool found2 = false;
for( std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it )
{
// pointless
// if( *it == crit1 ) found1 = true;
// if( *it == crit2 ) found2 = true;
if( it->someAttribute == crit1 ) found1 = true;
if( it->someAttribute == crit2 ) found2 = true;
if( found1 && found2 ) return true;
}
return false;
}
Comparison fixed, comapring an element to two different comparison arguments didn´t make sense
Thanks GNiewerth
Could you please explain to me this function, especially the const U& crit1 part
Many thanks
Since I don´t know what your data looks like I chose the type T for the data stored in the vector and type U for the find criteria. const is just a promise not to alter the variables during the function call.
If you´re having trouble understanding (const) references you should look it up in your C++ book or google for it.
That's exactly where I got confused
there is a vector of type string declared inside a struct and there are two string type variables declared inside the same struct. I want to check if both the string type variables are present in the string type vector? Could you please assist me on that.Code:// pointless
// if( *it == crit1 ) found1 = true;
// if( *it == crit2 ) found2 = true;
Many ThanksCode:struct aStruct
{
std::vector<std::string> vect;
std::string var1;
std::string var2;
};
Sure it does...? Isn't it the case where you have a vector of ints, and want to know if it contains both the value 3 and 5, for example?
Alternatively, here is an implementation using variadic template arguments. It allows asking a range if it contains a list of elements:
Code:#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
#include <array>
namespace
{
template <typename Iterator, typename IteratorBool, typename T>
void _search(Iterator it, IteratorBool it_bool, const T& val)
{
if(*it == val)
{*it_bool=true;}
}
template <typename Iterator, typename IteratorBool, typename T, typename... Args>
void _search(Iterator it, IteratorBool it_bool, const T& val, const Args&... args)
{
_search(it, it_bool, val);
_search(it, ++it_bool, args...);
}
}
template <typename Iterator, typename... Args>
bool contains(Iterator first, Iterator last, const Args&... args)
{
const size_t size = std::tuple_size<std::tuple<Args...>>::value;
std::array<bool, size> found{{}};
for( ; first!=last; ++first)
{
_search(first, found.begin(), args...);
}
return (std::find(found.cbegin(), found.cend(), false) == found.cend());
}
int main()
{
std::vector<int> vect{1,2,3,4,5,6,7,8,9};
auto first = vect.cbegin();
auto last = vect.cend();
std::cout << contains(first, last , 3, 5) << std::endl;
std::cout << contains(first, last , 1, 3, 5, 7) << std::endl;
std::cout << contains(first, last , 1, 3, 5, 7, 18) << std::endl;
std::cout << contains(first, last , 1) << std::endl;
std::cout << contains(first, last , 18) << std::endl;
std::cout << contains(first, last , 5) << std::endl;
}
Which would be great if any compilers actually supported variadic templates yet.
Monarch!
Can I test the program by running it as it is or do I need to make some changes. I want to see the output and then see it line by line.
Regards
Runs with my MinGW (GCC 4.5).
My code is largely experimental and for fun, and only runs with C++0x activated. It *should* run with no modifications (fully templated), but chances are it won't run on your platform without changing your environment. Don't waste your time and use GNiewerth's code.
My code aside, your problem is a simple loop, what part of GNiewerth's code are you having trouble with? Just replace his T/U with whatever type you are using.
Thanks GNiewerth and Monarch.
I have modified my code according to that of GNiewerth and it seems to be working :)
THANK YOU