Search() algorythm

This is a discussion on Search() algorythm within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use the algorithm Search() with strings. I get an error at the line where i marked. Could ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    812

    Search() algorythm

    I'm trying to use the algorithm Search() with strings.
    I get an error at the line where i marked.
    Could somebody please give me a hint what am i doing wrong? Thanks in advance!

    Code:
    int main () {
      vector<string> myvector ;
      vector<int>::iterator it;
      
      string str = "search";
      string match = "ea";
      myvector.push_back(str);
      
      it = search (myvector.begin(), myvector.end(), match.begin(), match.end());   // error
    
      if (it != myvector.end())
        cout << "Match found at position: " << int(it - myvector.begin()) << endl;
      else
        cout << "Match not found." << endl;
      
      system("pause");
      return 0;
    }
    Sorry for the typo: algorithm.
    Last edited by Ducky; 01-18-2008 at 02:08 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It appears that you are trying to compare single characters (what string iterators point to) with whole strings (what vector<string> iterator points to), and eventually assign the result to a vector<int> iterator?!

    If you just want to find a substring in a string, then string has the find method.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Thank you, i'm gonna check it if i can do it with find().

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Just out of interest, what would you have expected the output of your program to be?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    I would have expected something like: "Match found at position: 1" .

    So if i understand you well we should always compare the same types in C++.
    So maybe i should compare the first element of the vector to a string.
    Like this
    search (myvector[0].begin(), myvector[0].end(), match.begin(), match.end());
    Last edited by Ducky; 01-18-2008 at 01:22 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,183
    So, have you worked out a solution?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Like this
    search (myvector[0].begin(), myvector[0].end(), match.begin(), match.end());
    That looks more like it.

    However, if you only want to find a string in another string there may be easier ways: string reference
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Well for my final program i think im gonna use find() because it indeed does sound easier.

    For this specific program i dont understand why cant i assign search() to the iterator::it,
    when: "search() returns an iterator to the beginning of that matching subrange" .
    Last edited by Ducky; 01-18-2008 at 01:49 PM.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Is it still an iterator of vector<int>? Since you search in a string, the result of search can only be assigned to string::iterator.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Hmm thanks, i thought an iterator is always an int because its a number.
    I've changed the program but still get the error.
    Code:
    int main () {
      vector<string> myvector ;
      vector<string>::iterator it;
      
      string str = "search";
      string match = "ea";
      myvector.push_back(str);
      
      it = search (myvector[0].begin(), myvector[0].end(), match.begin(), match.end());   // error
    
      if (it != myvector[0].end())
        cout << "Match found at position: " << int(it - myvector[0].begin()) << endl;
      else
        cout << "Match not found." << endl;
      
      system("pause");
      return 0;
    }

  11. #11
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25

    C++ text book

    if you have an old college text book.

    they might have a bubble sort sample

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Ducky View Post
    Hmm thanks, i thought an iterator is always an int because its a number.
    An iterator is never just a number or an int. It is usually contains a pointer, and possibly has other members as well.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    @t3chn0n3rd bubble sort sample That rings me a bell, gotta look it up.

    @iMalc sounds like iterator is a complicated thing.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,183
    bubble sort sample That rings me a bell, gotta look it up.
    Go ahead and look it up to refresh your knowledge, but in this case I do not think it applies. If it did, you should use something like std::sort() from <algorithm> instead of implementing bubble sort.

    sounds like iterator is a complicated thing.
    It can be complicated, or it can be as simple as a pointer, since pointers are iterators.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are searching a string, not a vector. The string just happens to be put into a vector in this case (don't know why you need it there). Therefore the result needs to go to a string::iterator, not a vector<string>::iterator.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21