Iterator help

This is a discussion on Iterator help within the C++ Programming forums, part of the General Programming Boards category; In the for() loop i would like to give a new name to the iterator at every loop like: it1,it2,it3 ...

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

    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-02-2008 at 11:24 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,430
    C++ does not support variable variables. Why do you want to do that anyway?
    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

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    758
    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 02:00 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,435
    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
    758
    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 02: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
    758
    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 01:25 AM.

Popular pages Recent additions subscribe to a feed

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