Thread: Iterator help

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

    Iterator help

    In the for() loop i would like to give a new name to the iterator at every loop like: it1,it2,it3 etc.
    Would it be possible?
    Code:
    int main () 
    {        
       char* str;
       string str2 = "tom";
       ifstream file ("1.txt");
       int length;
      
         // get length of file:
        file.seekg (0, ios::end);
        length = file.tellg();
        file.seekg (0, ios::beg);
                
        // allocate memory:
        str = new char [length];
                
        // read data as a block:
        file.read (str,length);
        file.close();
        
        myvector.push_back(str);
        
        
        for (i = 0; i<5; i++)
        {
             it(i) = search (myvector[0].begin(), myvector[0].end(), str2.begin(), str2.end());
    
                  if (it(i) != myvector[0].end())
                  {
                       myvector[0][distance(myvector[0].begin(),it(i))] = 
                                        toupper(myvector[0][distance(myvector[0].begin(),it(i))]);
                  }    
        } 
       cout <<  myvector[0] << endl;       
       system("pause");
       return 0;
    }
    Last edited by Ducky; 02-03-2008 at 12:24 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    C++ does not support variable variables. Why do you want to do that anyway?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    To save place, because i want to find every occurrances of the string in the file and otherwise
    i should rewrite the search() uppercase() formula a lot of times.

    Could you suggest another approach to this problem? Thanks!
    Last edited by Ducky; 02-02-2008 at 03:00 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're not very specific.
    Save time? Rewrite formula?
    How would you use variable variables?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You have posted code similar to that.

    What is the purpose of making a vector that contains a single string? What makes you think that char* types have a begin() and end() method (and why aren't you using a real string)?

    I think I have once suggested to use the find() method of the string. To find all ocurrences of a string, use something like:

    Code:
        string source(...), target(...);
        size_t pos = 0;
        while ((pos = source.find(target, pos)) != string::npos) {
            //do something with source at pos
            pos += target.size();
        }
    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).

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Yes you already suggested to use find() but i thought that the find() method could only find the first occurrence.
    So i used search() to find more but search takes a vector as argument thats why i used vector[0].
    I couldnt compile search() with a string.

    Im using char* types because the read() method didnt compile with a string.

    Thanks!
    Last edited by Ducky; 02-02-2008 at 03:02 PM.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes you already suggested to use find() but i thought that the find() method could only find the first occurrence.
    If you look at the snippet I posted you'll see that find takes a second argument, the position to start search from. To find all occurrences you'd have to call find in a loop.
    Anyway, wouldn't search only find the first match, as well, and you'd have to call it in a loop to find next matches (passing a different start iterator)?

    So i used search() to find more but search takes a vector as argument thats why i used vector[0].
    No, algorithms take ranges, doesn't matter whether they are vectors or not. You could pass it a string iterators (or char*) just as well. In fact that is what you are doing. (Although you have a vector, you are only working with the item at vector[0] and that's a plain string.)

    Im using char* types because the read() method didnt compile with a string.
    Fine by me but it looks like your buffer doesn't have room for terminating 0 character.
    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
    932
    Thanks for the explanation, much appreciated!

    Quote Originally Posted by anon View Post
    Fine by me but it looks like your buffer doesn't have room for terminating 0 character.
    I guess what you mean is that i cant use istream::read() and string::find() together because istream::read() reads character by character.
    I hope there's a way to use them together it would be easier if you want to find several different targets on one line.
    Otherwise i should use getline() with putting the targets in a vector i guess.

    Edit : But its ok, i managed. Actually using getline() and vector takes up less place than using read().
    Last edited by Ducky; 02-03-2008 at 02:25 AM.

Popular pages Recent additions subscribe to a feed