C++ list方法怎么用

2024-11-18 09:35:42
推荐回答(2个)
回答1:

std::list strList;
strList.push_back(std::string("篮球"));
strList.push_back(std::string("羽毛球"));
strList.push_back(std::string("排球"));
for(std::list::iterator iter = strList.begin(); iter != strList.end(); ++iter)
// 还可以这样写,但是需要支持C++ 11标准的编译器
//for(auto iter = strList.begin(); iter != strList.end(); ++iter)

{
std::cout << *iter << std::endl;

}

回答2:

list LoveSports;
LoveSports.push_back("篮球");
LoveSports.push_back("羽毛球");
LoveSports.push_back("排球");

list::iterator it = LoveSports.begin();

for (; it != LoveSports.end(); it++)
{
cout<<*it<}