Iterators

This is a discussion on Iterators within the C++ Programming forums, part of the General Programming Boards category; Why does this code print what's in before twice?: Code: void element::printelementsplusgroupbefore (void) { map<std::string, int> temp; vector<map<std::string, int> >::iterator ...

  1. #1
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870

    Iterators

    Why does this code print what's in before twice?:
    Code:
    void element::printelementsplusgroupbefore (void)
    {
        map<std::string, int> temp;
        vector<map<std::string, int> >::iterator itr;
        map<std::string, int>::iterator mitr;
        int i = 1;
        for (itr = before.begin(); itr != before.end(); itr++)
        {
            temp = *itr;
            cout << "Plus group: " << i << endl;
            for (mitr = temp.begin(); mitr != temp.end(); ++mitr)
            {
                cout << "Element: " << mitr->first << " Count: " << mitr->second << endl;
            }
            temp.clear();
            i++;
        }
        cout << endl;
    }
    I give the input (processed elsewhere) of this:
    H2O2+C->*doesn't matter, not printed by this function*

    This function prints:

    Plus Group: 1
    Element: H Count: 2
    Element: O Count: 2
    Plus Group: 2
    Element: C Count: 1

    Plus Group: 1
    Element: H Count: 2
    Element: O Count: 2
    Plus Group: 2
    Element: C Count: 1

    Why does it print twice?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Could it be a problem with the before member variable?
    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
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Here's before:
    Code:
    vector<map<std::string, int> > before;
    Initialized with this function:
    Code:
    vector<map<std::string, int> > element::findelementplusgroupbefore(int* whereyield)
    {
        vector<map<std::string, int> > creed;
        map<std::string, int> need;
        std::string feed;
        for (int i = 0; i < formula.size(); i++)
        {
            if (formula[i] == '+')
            {
                creed.push_back(need);
                need.clear();
                feed.clear();
            }
            else if (isupper(formula[i]))
            {
                if(islower(formula[i+1]))
                {
                    if (isdigit(static_cast<int>(formula[i+2])))
                    {
                        feed += formula[i];
                        feed += formula[i+1];
                        need.insert(make_pair(feed, (static_cast<int>(formula[i+2])) - 48));
                        i += 2;
                    }
                    else
                    {
                        feed += formula[i];
                        feed += formula[i+1];
                        need.insert(make_pair(feed, 1));
                        i++;
                    }
                }
                else
                {
                    if (isdigit(static_cast<int>(formula[i+1])))
                    {
                        feed += formula[i];
                        need.insert(make_pair(feed, (static_cast<int>(formula[i+1])) - 48));
                        i++;
                    }
                    else
                    {
                        feed += formula[i];
                        need.insert(make_pair(feed, 1));
                    }
                }
                feed.clear();
            }
            else if (formula[i] == '-' && formula[i+1] == '>')
            {
                *whereyield = (formula[i+1]) - 57;
                break;
            }
        }
        creed.push_back(need);
        return creed;
    }
    And the constructor:
    Code:
    element::element()
    {
        cout << "Enter a formula:" << endl;
        cin >> formula;
        if (checkyield(formula))
        {
            before = findelementplusgroupbefore(&whereyield);
            after = findelementplusgroupafter(whereyield);
            printall();
        }
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,404
    Plus Group: 1
    The fact 1 is output through a counter that is not getting incremented, leads me to believe you are calling this function twice. The problem is somewhere else. Not within that code.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    I think that you should come up with the smallest and simplest compilable program that demonstrates the problem.

    Oh, and I agree with Mario F. - the function is probably being called twice.
    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

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Thanks Mario, that was the problem. In another part, a relic was calling that function.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  2. Using reverse iterators in algorithms
    By 0rion in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 02:19 AM
  3. Writing Iterators...
    By DeepBlackMagic in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM
  4. accessing vector elements: iterators are faster?
    By Captain Penguin in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2002, 01:27 PM
  5. Generic Progpammimg Iterators
    By rip1968 in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2002, 10:20 AM

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