【发布时间】:2010-09-30 23:07:54
【问题描述】:
例如,对于操作容器映射的两个线程,测试迭代器是否仍然有效(出于性能原因)的正确方法是什么?
或者只能通过间接的方式来做到这一点。
示例代码:
#define _SECURE_SCL 1
//http://msdn2.microsoft.com/en-us/library/aa985973.aspx
#define _SECURE_SCL_THROWS 1
#include "map"
#include "string"
#include "exception"
#include "iostream"
using namespace std;
void main(void)
{
map<string, string> map_test;
map<string, string>::iterator iter_map_test;
map_test [ "AAAAA" ] = "11111";
map_test [ "BBBBB" ] = "22222";
map_test [ "CCCCC" ] = "33333";
iter_map_test = map_test.find ("BBBBB");
map_test.erase ("BBBBB");
try
{
string value = (*iter_map_test).second;
}
catch ( exception & e )
{
cout << e.what() << endl;
}
catch ( ... )
{
cout << "generic exception." << endl;
}
}
【问题讨论】:
-
来自大师的更多信息:有效使用迭代器的三个指南aristeia.com/Papers/CUJ_June_2001.pdf
标签: c++ performance stl iterator