我試圖這樣做:maitain兩個向量的字符串,其中一個向量存儲值,第二個存儲相同值的引用。我認爲使用boost::reference_wrapper
會做的伎倆,但它似乎不會。我的平臺是VISUAL C++ 2008您可以使用boost :: reference_wrapper將引用存儲在STL容器中嗎?
std::vector<std::string> str_vec;
str_vec.push_back("abc");
str_vec.push_back("cde");
str_vec.push_back("fgh");
std::vector<boost::reference_wrapper<std::string> > str_view;
for(std::vector<std::string>::iterator it = str_vec.begin(); it != str_vec.end(); ++it)
{
str_view.push_back(*it);
}
這是錯誤:
error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from std::basic_string<_Elem,_Traits,_Ax>' to 'const boost::reference
我可以使用boost::shared_ptr
但我想到了一個更好的參考表達我的意圖。此代碼可能可以在C++ 11中使用std::reference_wrapper
工作,但目前尚不可用。
什麼是visual C++ 2009?據我所知,有2008年,2010年和2011年的測試版。 – EdChum 2012-04-22 20:56:13