Results 1 to 6 of 6

Thread: How can I use the find_first_of() in C++

  1. #1
    Join Date
    Dec 2009
    Posts
    31

    How can I use the find_first_of() in C++

    Hello, Everybody. I am the student of the MSC-IT second year. I have to create the on the find_first_of() function of the C++ language. I can have the knowledge of the functions that can be in-built in the the C++ language. So, I want to know about the find_first_of() function of the C++ function. I also want to know how the find_first_of() function can be used in the C++ language. Can anyone has the solution on find_first_of() function then reply me!!

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Use the find_first_of() in C++

    The find_first_of() function that can be used to find an element from the given set in specified range. The following can be the general syntax of the find_first_of() function as follows :
    template <class FrwrdItrtr1, class FrwrdItrtr2>
    FrwrdItrtr1 find_first_of ( FrwrdItrtr1 fst1, FrwrdItrtr1 lst1, FrwrdItrtr2 fst2, FrwrdItrtr2 lst2 );

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    The find_first_of()

    The following code of lines demonstrates you about find_first_of() function in the C++ language As :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <cctype>
    #include <vector>
    using namespace std;
    bool compcaseinsensitive (char D1, char D2) 
    {
      return (tolower(D1)==tolower(D2));
    }
    int main () 
    {
      int mchrs[] = {'a','b','c','A','B','C'};
      vector<char> mvctr (mchrs,mchrs+6);
      vector<char>::iterator t;
      int match[] = {'A','B','C'};
      t = find_first_of (mvctr.begin(), mvctr.end(), match, match+3);
      if (t!=mvctr.end())
        cout << "first match is: " << *t << endl;
      t = find_first_of (mvctr.begin(), mvctr.end(),
                          match, match+3, compcaseinsensitive);
      if (t!=mvctr.end())
        cout << "first match is: " << *t << endl;
     return 0;
    }
    The above code of lines can generate the following Output :
    Code:
    First match is: A
    First match is: a

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How can I use the find_first_of() in C++

    There can be one more example on the find_first_of() function of C++ language for you :
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    #include <string>
    using std::string;
    int main()
    {
       string strng1( "This is a test string!");
       int lct;
       lct = strng1.find_first_of( "is" );
       cout << "\n\n(find_first_of) found '" << strng1[ lct ]
          << "at: " << lct;
      return 0;
    }
    The following can be the out put of the above code :
    Code:
    (find_first_of) found 'iat: 2"

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    The find_first_of()

    The following can be the parameters of the function find_first_of() as listed below :
    1. fst2, lst2 : These variables can be used for the forward iterators to the final and starting positions of an element values that can be searched for. The range used can be as [fst2,lst2).
    2. fst1, lst1 : These variables can be used for forward iterators to the final and starting positions of the sequence that can be searched. The range used can be as [fst1,lst1).

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: How can I use the find_first_of() in C++

    Program :
    Code:
          #include <algorithm>   
          #include <functional>   
          #include <iostream>     
          #include <vector>       
          int main ()
          {
               typedef std::vector<int, std::allocator<int> > vector;
              const vector::value_type a1[] = 
    { 
                  0, 1, 2, 2, 3, 4, 2, 2, 6, 7 
              };
              const vector::value_type a2[] = { 6, 4 };
              const vector v1(a1, a1 + sizeof a1 / sizeof *a1);
              const vector v2(a2, a2 + sizeof a2 / sizeof *a2);
              vector::const_iterator it1 =
                  std::find_first_of(v1.begin(), v1.end(), 
                                     v2.begin(), v2.end()); 
              vector::const_iterator it2 =
                  std::find_first_of(v1.begin(), v1.end(), 
                                     v2.begin(), v2.end(),
                                  std::equal_to<vector::value_type>());
              std::ostream_iterator<vector::value_type, char,
                  std::char_traits<char> > out(std::cout, " " );
              std::cout << "For the vectors { ";
              std::copy(v1.begin(), v1.end(), out);
              std:: cout << "} and { ";
              std::copy(v2.begin(), v2.end(), out);
              std::cout << "}\nboth versions of find_first_of "
                        << "point to: " << *it1 << std::endl; 
              return !(*it1 == *it2);
          }
    Output :
    Code:
          For the vectors { 0 1 2 2 3 4 2 2 6 7 } and { 6 4 }
          both versions of find_first_of point to: 4

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,986,573.67618 seconds with 15 queries