简体   繁体   English

std :: set :: iterator的大小是否有限制?

[英]Is there a limit on the size of std::set::iterator?

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. 我有一个std :: set字符串,我想遍历它们,但是对于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, 该代码工作正常& *iter被取消引用和分配word ,直到大小KeywordsDictionary大约是15000然而,当大小KeywordsDictionary超过15000的增加,

  1. the print statement cout << *iter; 打印语句cout << *iter; is printing all the contents of KeywordsDictionary correctly. 正在正确打印KeywordsDictionary所有内容。
  2. but the pointer to the iterator *iter is not being dereferenced & not being assigned to word . 但指向迭代器*iter的指针未取消引用且未分配给word word is just being an empty string. word只是一个空字符串。

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. 所以基本上,我猜循环只会执行一次。

Try to declare keyword_len as 尝试将keyword_len声明为

std::string::size_type keyword_len = 0;

instead of 代替

int keyword_len = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM