我正在通過值列表進行搜索,並希望當它無法找到要搜索的值時返回某種「默認」錯誤值。我不是說std::cerr
。我的意思是我的價值,我可以在我的程序中使用像布爾。但是,如果它找到了字符串,我需要它在字符串中的位置的指針值。我該怎麼做呢?如何獲得std :: list <std::string> :: iterator來輸出錯誤值?
2
A
回答
5
typedef std::list<std::string> MyList;
MyList mylist;
MyList::iterator iter = mylist.find("somestring");
if (iter != mylist.end()) {
// the string was found
} else {
// the string was not found
}
4
慣用的方式是返回一個迭代器(而不是指針),並在元素未找到時返回結束迭代器。
但是你不需要寫這個,因爲<algorithm>
已經包含std::find
。
相關問題
- 1. std :: list :: clear invalidate std :: list :: end iterator?
- 2. 如何從程序獲得std輸出
- 3. 'list iterator not derefereenable'
- 4. 如何爲std :: list重載std :: remove?
- 5. 'std :: list <_Ty> :: push_back':無法將'this'指針從'const std :: list <_Ty>'轉換爲'std :: list <_Ty>''
- 6. 如何訪問std :: list中的個人'類型'std :: list < type >>
- 7. C++ std :: list分段錯誤
- 8. Iterator,ListIterator和List
- 9. std :: iterator的錯誤指示器
- 10. C++模板std :: vector :: iterator錯誤
- 11. 如何解決'std :: list <int> *'賦值爲'std :: list <int> [0]'的不兼容類型?
- 12. std :: string :: append(std :: string)錯誤的輸出
- 13. 使用std :: make_move_iterator將std :: list <std :: unique_ptr >>插入另一個時出錯
- 14. @ spring.formInput #list iterator
- 15. 如何獲得List的單個值<object>
- 16. 爲什麼我不能獲得std :: function <std :: vector <T> :: iterator>綁定到返回該類型的lambda?
- 17. 如何從std :: vector <>
- 18. log4net如何獲得Log錯誤的輸出?
- 19. 如何獲得std :: numeric_limits <char> :: min()的值?
- 20. 在std :: list的指針上調用一個函數<Shape*> :: iterator
- 21. print unordered_map <std :: string,std :: list <std::string>
- 22. 實現std :: iterator
- 23. 迭代std :: list <boost :: variant>
- 24. 獲得錯誤的輸出數組
- 25. 擴展std :: list
- 26. 爲什麼Visual Studio使用std :: iterator <>而不是mine :: iterator <>
- 27. 錯誤:無法將'dirent *'轉換爲'search_directories(std :: string,std :: list <start_tournament_info_t *>&,bool):: direct *'
- 28. 如何獲得std :: endl在必要時輸出「\ r \ n」?
- 29. 重載<<運算符打印出一個std :: list
- 30. C++「list iterator not dereferenceable」error
'std :: cerr'遠遠不像「錯誤值」;它是一個**位置(流),您可以在其中輸出錯誤消息**。 – 2011-03-07 18:08:00