不能与每个人一起工作

Cant work with for each

本文关键字:工作 一起 每个人 不能      更新时间:2023-10-16
#include <iostream>
using namespace std;
#include <string>
// Sequence containers
#include <list>
#include <vector>
int main() {
string sve[MAX] = { "one","two", "three", "four", "five" };
//2
vector <string> vstr;
//3
for(int i=0;i<MAX;i++)
    vstr.push_back(sve[i]);
//4
cout<<"---Vector---n";
for each (string s in vstr)
    cout<<s<<endl;

错误:预期的"("之后。错误发生在 对于每一行

我不认为我缺少任何包含,这很奇怪。 Xcode 4.3 上的 IM

语法错误。试试这个:

for(string s : vstr)
    cout<<s<<endl;

顺便说一下,与其初始化数组,然后复制到 vector,不如一步到位,并熟练地创建数组:

std::vector<std::string> sve{ "one","two", "three", "four", "five" };

是无效的 C++ - 不编译是放弃

也许看看 http://www.cplusplus.com/reference/algorithm/for_each/就是你在想的