0

I have a std::set of strings and I want to iterate over them, but the iterator is behaving differently for different sizes of set. Given below is the code snippet that I'm working on:

int test(set<string> &KeywordsDictionary){
    int keyword_len = 0;
    string word;
    set<string>::iterator iter;

    cout << "total words in the database : " << KeywordsDictionary.size() << endl;

    for(iter=KeywordsDictionary.begin();iter != KeywordsDictionary.end();iter++) {

        cout << *iter;

        word = *iter;
        keyword_len = word.size();

        if(keyword_len>0)
            Dosomething();
        else
            cout << "Length of keyword is <= 0" << endl;
    }
    cout << "exiting test program"  << endl;
}

The code is working properly & *iter is being dereferenced & assigned to word until the size of KeywordsDictionary is around 15000. However when the size of KeywordsDictionary increases beyond 15000,

  1. the print statement cout << *iter; is printing all the contents of KeywordsDictionary correctly.
  2. but the pointer to the iterator *iter is not being dereferenced & not being assigned to word. word is just being an empty string.

EDIT: And the output of the program is :

total words in the database : 22771
�z���AAAADAAIIABABBABLEABNABOUTACACCEPTEDACCESSACCOUNT...
Length of keyword is <= 0
exiting test program

So basically, I'm guessing the loop is executing only once.

annunarcist
  • 1,637
  • 3
  • 20
  • 42
  • `std::set::iterator` is a fixed size, regardless of the size of the contents held in the `T` object. – Chad Nov 27 '13 at 19:38
  • Please present a complete example that demonstrates the problem, copied and pasted from something you actually compiled and ran. – Benjamin Lindley Nov 27 '13 at 19:39
  • You don' seem to have a newline at the end of printing the string (`cout << *iter;`), so all the strings will be printed one after another on the same line? Is that what you see? – rabensky Nov 27 '13 at 20:14
  • I think the problem is because of non-ASCII values such as : `�z���` and assigning them to `word` which is of string type whose range is only char type and ASCII values. Correct me if I'm wrong. – annunarcist Nov 27 '13 at 21:57
  • and whenever its reading a non-ASCII value, the string variable `word` is being empty – annunarcist Nov 27 '13 at 23:03

1 Answers1

0

Try to declare keyword_len as

std::string::size_type keyword_len = 0;

instead of

int keyword_len = 0;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • If the threshold for problems really is as low as 15000, this seems unlikely culprit. – hyde Nov 27 '13 at 20:10