带有模板参数的 c++ Array class 模板
c++ Array class template with template parameters
我已经创建了一个数组 class 模板,模板参数为 和
数组 class 个成员、输入、排序和输出函数。
但是代码在下面不起作用我可能做错了什么?
#include <iostream>
using namespace std;
template <class T, int n>
class array {
T mass[n];
public:
void input();
void output();
void sort();
};
template <class T, int n>
void array < T, n > ::input() {
for (int i = 0; i < n; i++)
cin >> mass[i];
}
template <class T, int n>
void array < T, n > ::output() {
for (int i = 0; i < n; i++)
cout << mass[i] << '[=11=]';
}
template <class T, int n>
void array < T, n > ::sort()[T x; int p = 1, m = n;
while (p) {
p = 0;
for (int i = 0; i < m - 1; i++)
if (mas[i] > mas[i + 1])
[x = mass[i]; mass[i] = mass[i + 1]; mass[i + 1] = x; p = 1;
}
m--;
}
}
int main() {
array < int, 10 > a;
array < float, 5 > b;
a.input();
a.sort();
a.output();
b.input();
b.sort();
b.output();
return 0;
}
我收到以下编译器错误 我在这段代码中可能做错了什么?
25 | void array < T, n > ::sort()[T x; int p = 1, m = n;
| ^
/tmp/ZOMErK6tKN.cpp:25:33: error: expected ']' before 'x'
25 | void array < T, n > ::sort()[T x; int p = 1, m = n;
| ^~
| ]
/tmp/ZOMErK6tKN.cpp:25:52: error: 'n' was not declared in this scope
25 | void array < T, n > ::sort()[T x; int p = 1, m = n;
| ^
/tmp/ZOMErK6tKN.cpp:26:5: error: expected unqualified-id before 'while'
26 | while (p) {
| ^~~~~
/tmp/ZOMErK6tKN.cpp:32:7: error: 'm' does not name a type
32 | m--;
| ^
/tmp/ZOMErK6tKN.cpp:33:5: error: expected declaration before '}' token
33 | }
| ^
/tmp/ZOMErK6tKN.cpp:34:3: error: expected declaration before '}' token
34 | }```
这一行:
void array < T, n > ::sort()[T x; int p = 1, m = n;
[
应该是 {
另外我认为你可以通过使用标准库来实现这种排序
void array < T, n > ::sort() {
std::sort(std::begin(mass), std::end(mass));
}
您的代码中有一些拼写错误。特别是,您使用 [
而不是 {
和 mas
而不是 mass
。这些是正确的,并使用以下代码中的注释突出显示:
template <class T, int n>
//----------------------------v------------------->[ changed to {
void array < T, n > ::sort(){T x; int p = 1, m = n;
while (p) {
p = 0;
for (int i = 0; i < m - 1; i++)
//----------vvvv------vvvv----------------------->mas changed to mass
if (mass[i] > mass[i + 1])
//--------v-------------------------------------->[ changed to {
{x = mass[i]; mass[i] = mass[i + 1]; mass[i + 1] = x; p = 1;
}
m--;
}
}
我已经创建了一个数组 class 模板,模板参数为
但是代码在下面不起作用我可能做错了什么?
#include <iostream>
using namespace std;
template <class T, int n>
class array {
T mass[n];
public:
void input();
void output();
void sort();
};
template <class T, int n>
void array < T, n > ::input() {
for (int i = 0; i < n; i++)
cin >> mass[i];
}
template <class T, int n>
void array < T, n > ::output() {
for (int i = 0; i < n; i++)
cout << mass[i] << '[=11=]';
}
template <class T, int n>
void array < T, n > ::sort()[T x; int p = 1, m = n;
while (p) {
p = 0;
for (int i = 0; i < m - 1; i++)
if (mas[i] > mas[i + 1])
[x = mass[i]; mass[i] = mass[i + 1]; mass[i + 1] = x; p = 1;
}
m--;
}
}
int main() {
array < int, 10 > a;
array < float, 5 > b;
a.input();
a.sort();
a.output();
b.input();
b.sort();
b.output();
return 0;
}
我收到以下编译器错误 我在这段代码中可能做错了什么?
25 | void array < T, n > ::sort()[T x; int p = 1, m = n;
| ^
/tmp/ZOMErK6tKN.cpp:25:33: error: expected ']' before 'x'
25 | void array < T, n > ::sort()[T x; int p = 1, m = n;
| ^~
| ]
/tmp/ZOMErK6tKN.cpp:25:52: error: 'n' was not declared in this scope
25 | void array < T, n > ::sort()[T x; int p = 1, m = n;
| ^
/tmp/ZOMErK6tKN.cpp:26:5: error: expected unqualified-id before 'while'
26 | while (p) {
| ^~~~~
/tmp/ZOMErK6tKN.cpp:32:7: error: 'm' does not name a type
32 | m--;
| ^
/tmp/ZOMErK6tKN.cpp:33:5: error: expected declaration before '}' token
33 | }
| ^
/tmp/ZOMErK6tKN.cpp:34:3: error: expected declaration before '}' token
34 | }```
这一行:
void array < T, n > ::sort()[T x; int p = 1, m = n;
[
应该是 {
另外我认为你可以通过使用标准库来实现这种排序
void array < T, n > ::sort() {
std::sort(std::begin(mass), std::end(mass));
}
您的代码中有一些拼写错误。特别是,您使用 [
而不是 {
和 mas
而不是 mass
。这些是正确的,并使用以下代码中的注释突出显示:
template <class T, int n>
//----------------------------v------------------->[ changed to {
void array < T, n > ::sort(){T x; int p = 1, m = n;
while (p) {
p = 0;
for (int i = 0; i < m - 1; i++)
//----------vvvv------vvvv----------------------->mas changed to mass
if (mass[i] > mass[i + 1])
//--------v-------------------------------------->[ changed to {
{x = mass[i]; mass[i] = mass[i + 1]; mass[i + 1] = x; p = 1;
}
m--;
}
}