[英]Is there a limit on the size of std::set::iterator?
我有一個std :: set字符串,我想遍歷它們,但是對於set的不同大小,迭代器的行為有所不同。 下面給出的是我正在處理的代碼片段:
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;
}
該代碼工作正常& *iter
被取消引用和分配word
,直到大小KeywordsDictionary
大約是15000然而,當大小KeywordsDictionary
超過15000的增加,
cout << *iter;
正在正確打印KeywordsDictionary
所有內容。 *iter
的指針未取消引用且未分配給word
。 word
只是一個空字符串。 編輯:並且該程序的輸出是:
total words in the database : 22771
�z���AAAADAAIIABABBABLEABNABOUTACACCEPTEDACCESSACCOUNT...
Length of keyword is <= 0
exiting test program
所以基本上,我猜循環只會執行一次。
嘗試將keyword_len聲明為
std::string::size_type keyword_len = 0;
代替
int keyword_len = 0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.