|
-
January 31st, 2011, 04:33 AM
#1
finding 2 elements in one vector iteration
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
-
January 31st, 2011, 04:46 AM
#2
Re: finding 2 elements in one vector iteration
Basic approach:
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;
}
Edit:
Comparison fixed, comapring an element to two different comparison arguments didn´t make sense
Last edited by GNiewerth; January 31st, 2011 at 08:28 AM.
Reason: Code fixed
- Guido
-
January 31st, 2011, 06:26 AM
#3
Re: finding 2 elements in one vector iteration
Thanks GNiewerth
Could you please explain to me this function, especially the const U& crit1 part
Many thanks
-
January 31st, 2011, 08:19 AM
#4
Re: finding 2 elements in one vector iteration
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.
- Guido
-
January 31st, 2011, 08:43 AM
#5
Re: finding 2 elements in one vector iteration
That's exactly where I got confused
Code:
// pointless
// if( *it == crit1 ) found1 = true;
// if( *it == crit2 ) found2 = true;
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:
struct aStruct
{
std::vector<std::string> vect;
std::string var1;
std::string var2;
};
Many Thanks
-
January 31st, 2011, 09:03 AM
#6
Re: finding 2 elements in one vector iteration
 Originally Posted by GNiewerth
Edit:
Comparison fixed, comapring an element to two different comparison arguments didn´t make sense
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;
}
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
January 31st, 2011, 09:10 AM
#7
Re: finding 2 elements in one vector iteration
Which would be great if any compilers actually supported variadic templates yet.
-
January 31st, 2011, 09:13 AM
#8
Re: finding 2 elements in one vector iteration
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
-
January 31st, 2011, 09:24 AM
#9
Re: finding 2 elements in one vector iteration
 Originally Posted by Lindley
Which would be great if any compilers actually supported variadic templates yet.
Runs with my MinGW (GCC 4.5).
 Originally Posted by gulHK
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
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
January 31st, 2011, 10:14 AM
#10
Re: finding 2 elements in one vector iteration
Thanks GNiewerth and Monarch.
I have modified my code according to that of GNiewerth and it seems to be working 
THANK YOU
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|