C++模板数组类三行输入
时间: 2023-09-16 07:10:01 浏览: 47
下面是一个简单的 C++ 模板数组类的实现,可以用三行输入来创建和初始化一个数组对象:
```c++
#include <iostream>
template <class T, int size>
class Array {
private:
T arr[size];
public:
void input() {
for (int i = 0; i < size; i++)
std::cin >> arr[i];
}
void output() {
for (int i = 0; i < size; i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
}
};
int main() {
Array<int, 5> a;
std::cout << "Please input 5 integers:" << std::endl;
a.input();
std::cout << "The array is:";
a.output();
return 0;
}
```
在该示例中,我们使用了一个模板类 `Array`,它接受两个参数:`T` 表示元素类型,`size` 表示元素个数。在 `main` 函数中,我们创建了一个 `Array<int, 5>` 类型的对象 `a`,并要求用户输入 5 个整数,最后输出这个数组。
相关问题
C++模板数组类三行输入:第一行(1,2,3)确定数组类型;第二行int类型确定数组的下标,第三行确定数组元素的值
下面是一个根据输入确定数组类型、下标和元素值的 C++ 模板数组类的实现,可以用三行输入来创建和初始化一个数组对象:
```c++
#include <iostream>
template <typename T>
class Array {
private:
T* arr;
public:
Array(int size) {
arr = new T[size];
}
void input(int index, T value) {
arr[index] = value;
}
void output() {
for (int i = 0; i < sizeof(arr)/sizeof(T); i++)
std::cout << arr[i] << " ";
std::cout << std::endl;
}
};
int main() {
std::string type;
int index;
std::string values;
std::cout << "Please input the type of the array: ";
std::cin >> type;
std::cout << "Please input the index of the array: ";
std::cin >> index;
std::cout << "Please input the values of the array: ";
std::cin >> values;
Array<std::string>* a = new Array<std::string>(index);
std::string delimiter = ",";
size_t pos = 0;
std::string token;
int i = 0;
while ((pos = values.find(delimiter)) != std::string::npos) {
token = values.substr(0, pos);
a->input(i, token);
values.erase(0, pos + delimiter.length());
i++;
}
a->input(i, values);
std::cout << "The array is: ";
a->output();
delete a;
return 0;
}
```
在该示例中,我们使用了一个模板类 `Array`,它接受一个参数 `T` 表示元素类型。在 `main` 函数中,我们首先要求用户输入数组类型 `type`,下标 `index` 和元素值 `values`,然后根据输入创建一个 `Array<std::string>` 类型的对象 `a`,并将元素值分割后逐一输入数组中。最后输出这个数组。
C++模板数组类
以下是一个简单的 C++ 模板数组类的示例代码:
```c++
#include <iostream>
template <typename T, int size>
class Array {
private:
T arr[size];
public:
void set(int index, T value) {
arr[index] = value;
}
T get(int index) const {
return arr[index];
}
};
int main() {
Array<int, 5> arr;
for (int i = 0; i < 5; i++) {
arr.set(i, i);
}
for (int i = 0; i < 5; i++) {
std::cout << arr.get(i) << " ";
}
return 0;
}
```
该类模板有两个模板参数:T 表示数组元素的类型,size 表示数组的大小。在该模板类中,我们定义了两个公有成员函数:set 和 get,用于设置和获取数组元素。
在主函数中,我们使用了该模板类来创建了一个整数类型的长度为 5 的数组,并通过 set 函数将数组元素设置为数组下标的值。最后,我们使用 get 函数输出了数组元素的值。
需要注意的是,在实例化模板类时,需要显式地指定模板参数的类型和值。在本例中,我们通过 Array<int, 5> 来实例化了该模板类。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)