CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2010
    Posts
    146

    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

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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&#180;t make sense
    Last edited by GNiewerth; January 31st, 2011 at 08:28 AM. Reason: Code fixed
    - Guido

  3. #3
    Join Date
    Nov 2010
    Posts
    146

    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

  4. #4
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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

  5. #5
    Join Date
    Nov 2010
    Posts
    146

    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

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: finding 2 elements in one vector iteration

    Quote Originally Posted by GNiewerth View Post
    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.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: finding 2 elements in one vector iteration

    Which would be great if any compilers actually supported variadic templates yet.

  8. #8
    Join Date
    Nov 2010
    Posts
    146

    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

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: finding 2 elements in one vector iteration

    Quote Originally Posted by Lindley View Post
    Which would be great if any compilers actually supported variadic templates yet.
    Runs with my MinGW (GCC 4.5).

    Quote Originally Posted by gulHK View Post
    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.

  10. #10
    Join Date
    Nov 2010
    Posts
    146

    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
  •  





Click Here to Expand Forum to Full Width

Featured