提问人:ashamat 提问时间:8/11/2022 最后编辑:user12002570ashamat 更新时间:8/11/2022 访问量:95
语法结构 C{int x;} 是什么意思无孔;C++ 中的平均值 [重复]
What does the syntax struct C{int x;}var; mean in C++ [duplicate]
问:
我正在查看有关 std::for_each 的文档。(https://cplusplus.com/reference/algorithm/for_each/)
我无法理解他们使用 的方式,因为他们没有声明要传递给的变量。
这是代码:struct myclass
main
for_each
#include <iostream> // std::cout
#include <algorithm> // std::for_each
#include <vector> // std::vector
struct myclass { // function object type:
void operator() (int i) {std::cout << ' ' << i;}
} myobject;
int main () {
std::vector<int> myvector;
myvector.push_back(10);
myvector.push_back(20);
myvector.push_back(30);
std::cout << "myvector contains:";
for_each (myvector.begin(), myvector.end(), myobject);
std::cout << '\n';
return 0;
}
我的直觉是
struct myclass { // function object type:
void operator() (int i) {std::cout << ' ' << i;}
} myobject;
等于:
struct myclass { // function object type:
void operator() (int i) {std::cout << ' ' << i;}
};
myclass myobject;
因此,他们声明变量(作为全局变量)并在同一语句中定义结构。这是对的吗?
答: 暂无答案
评论
myclass myobject;
myobject
class
struct
struct
class
struct
public: