std::set::迭代器的大小有限制吗



我有一个std::字符串集,我想对它们进行迭代,但迭代器对不同大小的字符串集的行为不同。下面是我正在处理的代码片段:

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;
}

代码工作正常&CCD_ 1正在被取消引用&分配给CCD_ 2,直到CCD_。然而,当KeywordsDictionary的大小增加到15000以上时,

  1. 打印语句CCD_ 5正在正确地打印CCD_
  2. 但是指向迭代器CCD_ 7的指针没有被取消引用&不被分配给CCD_ 8。word只是一个空字符串

编辑:程序的输出为:

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

所以基本上,我猜这个循环只执行一次。

尝试将关键字_len声明为

std::string::size_type keyword_len = 0;

而不是

int keyword_len = 0;

最新更新