```c++
#include <bits/stdc++.h>
using namespace std;
set s;
int main()
{
int x;
cin>>x;
s.insert(x);
for(auto v: s) cout<<v<<endl;
return 0;
}
```for语句报错了》。。。。。
range-for
是C++11新语法,你得确定你的编译器支持C++11,并打开C++11选项,比如:
#include <iostream>
#include <set>
int main()
{
std::set<int> s = {1, 2, 3, 3, 4};
for (auto v : s)
std::cout << v << ' ';
std::cout << std::endl;
return 0;
}
$ g++ -Wall -std=c++11 main.cpp
$ ./a.out
1 2 3 4
for(auto v: s)这是c++ 11的新特性.如果你的编译器不是c++11会报for(auto v: s)语法错误
参考代码
#include<iostream>
#include<set>
using namespace std;
set<int> s;
int main()
{
int x,i;
for (i = 0; i < 5; i++)
{
cin>>x;
s.insert(x);
}
for(auto v: s) cout<<v<<endl;
return 0;
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!