举几个函数结构体的例子
时间: 2023-11-08 14:56:26 浏览: 19
C++中的函数结构体是一种可以像普通函数一样使用的结构体。它可以通过重载运算符来实现,也可以通过定义函数指针来实现。
举个例子:
```
struct Add {
int operator()(int a, int b) const {
return a + b;
}
};
Add add;
int result = add(3, 4); // result is 7
```
另一个例子:
```
struct Divide {
double operator()(double a, double b) const {
return a / b;
}
};
Divide divide;
double result = divide(10.0, 3.0); // result is 3.3333
```
还有一个例子
```
struct Multiply {
int operator()(int a, int b) const {
return a * b;
}
};
Multiply multiply;
int result = multiply(2, 3); // result is 6
```
相关问题
写一个用结构体当 key 的 std:: map 的 c++的例子
### 回答1:
可以的,以下是一个使用结构体作为 key 的 std::map 的 C++ 代码示例:
```c++
#include <iostream>
#include <map>
#include <string>
using namespace std;
struct Student {
string name;
int age;
double score;
bool operator<(const Student& other) const {
return name < other.name;
}
};
int main() {
map<Student, int> studentMap;
Student s1 = {"Tom", 18, 90.5};
Student s2 = {"Jerry", 19, 88.5};
Student s3 = {"Alice", 20, 95.};
studentMap[s1] = 1;
studentMap[s2] = 2;
studentMap[s3] = 3;
for (auto& it : studentMap) {
cout << it.first.name << " " << it.first.age << " " << it.first.score << " " << it.second << endl;
}
return ;
}
```
这个例子中,我们定义了一个结构体 `Student`,包含了学生的姓名、年龄和成绩。我们重载了 `<` 运算符,以便能够在 `map` 中使用结构体作为 key。然后我们创建了一个 `map`,将三个学生作为 key,对应的值分别为 1、2、3。最后我们遍历了这个 `map`,输出了每个学生的信息和对应的值。
### 回答2:
下面是一个使用结构体作为std::map的键的C++例子:
```cpp
#include <iostream>
#include <map>
// 声明一个结构体作为std::map的键
struct Key {
int x;
int y;
};
bool operator<(const Key& lhs, const Key& rhs) {
if (lhs.x != rhs.x) return lhs.x < rhs.x;
return lhs.y < rhs.y;
}
int main() {
std::map<Key, int> myMap;
// 向map中插入键值对
myMap.insert({{1, 2}, 10});
myMap.insert({{3, 4}, 20});
myMap.insert({{5, 6}, 30});
// 访问map中的元素
std::cout << "myMap[{1, 2}] = " << myMap[{1, 2}] << std::endl;
std::cout << "myMap[{5, 6}] = " << myMap[{5, 6}] << std::endl;
// 遍历map中的元素
for (const auto& pair : myMap) {
std::cout << "(" << pair.first.x << ", " << pair.first.y << "): " << pair.second << std::endl;
}
return 0;
}
```
这个例子中,我们定义了一个结构体`Key`作为`std::map`的键。为了让`std::map`正确比较这些结构体对象,我们重载了`<`操作符。在`main`函数中,我们创建了一个`myMap`对象,并向其中插入了一些键值对。我们可以通过索引访问map中的元素,并使用范围for循环遍历整个map并输出各个键和对应的值。
运行结果如下:
```
myMap[{1, 2}] = 10
myMap[{5, 6}] = 30
(1, 2): 10
(3, 4): 20
(5, 6): 30
```
### 回答3:
下面是一个使用结构体作为key的示例代码:
```c
#include <iostream>
#include <map>
#include <string>
struct Student {
std::string name;
int age;
double score;
// 重载小于运算符,用于比较结构体对象的排序
bool operator<(const Student& other) const {
if (name != other.name) {
return name < other.name;
} else if (age != other.age) {
return age < other.age;
} else {
return score < other.score;
}
}
};
int main() {
std::map<Student, std::string> studentMap;
// 创建几个学生对象作为key,对应学生姓名作为value
Student student1 = {"Alice", 18, 90.0};
Student student2 = {"Bob", 19, 80.0};
Student student3 = {"Alice", 18, 95.0};
// 插入键值对到map中
studentMap[student1] = "Class A";
studentMap[student2] = "Class B";
studentMap[student3] = "Class A";
// 打印map中的键值对
for (const auto& pair : studentMap) {
std::cout << "姓名: " << pair.first.name << ", 年龄: "
<< pair.first.age << ", 分数: "
<< pair.first.score << ", 班级: "
<< pair.second << std::endl;
}
return 0;
}
```
以上代码演示了如何使用结构体`Student`作为`std::map`的key,并且给每个学生对应一个班级。通过重载`<`运算符,可以实现结构体对象的比较和排序。最后,通过迭代器遍历`std::map`中的每个键值对,并输出对应的学生信息和班级。
c语言extern 结构体
在 C 语言中,extern 关键字用于声明一个变量或函数是在其他文件中定义的,而不是当前文件中定义的。在结构体中使用 extern 关键字时,需要注意以下几点:
1. 结构体本身不能用 extern 关键字来声明,只能用 typedef 或 struct 关键字来定义结构体类型。
2. 如果在定义结构体类型时使用了 extern 关键字,那么该结构体类型的定义必须在其它文件中进行,而不是在当前文件中。
3. 如果在结构体类型定义中包含了结构体成员变量,并且这些成员变量的类型是其他文件中定义的结构体类型,那么需要使用 extern 关键字来声明这些成员变量的类型。
下面是一个例子:
在文件 A 中定义了一个结构体类型,并使用 extern 关键字声明了其中一个成员变量的类型:
```c
// File A
struct student {
char name[20];
int age;
extern struct score; // 声明 score 的类型是在其他文件中定义的
};
typedef struct student STU;
```
在文件 B 中定义了另一个结构体类型,并定义了 score 变量:
```c
// File B
struct score {
int math;
int english;
};
struct score s = {80, 90};
```
在文件 C 中使用了结构体类型 STU,并定义了一个变量 stu:
```c
// File C
#include <stdio.h>
#include "A.h" // 包含结构体类型的定义
int main() {
STU stu = {"Tom", 18, s}; // 使用定义在文件 B 中的 score 变量
printf("Name: %s, Age: %d, Math: %d, English: %d\n", stu.name, stu.age, stu.score.math, stu.score.english);
return 0;
}
```
在上面的例子中,通过使用 extern 关键字声明了 score 变量的类型,使得在结构体类型 STU 中可以直接使用 score 变量。最后在文件 C 中使用了 STU 结构体类型和 score 变量,输出了 stu 变量的值。