new char*[n+1]是什么意思
时间: 2023-05-30 22:04:15 浏览: 49
这个语法是C++中动态分配一个指向字符型指针的数组,数组的长度为n。其中,n是一个整数,表示数组的长度。指针数组是一个包含指针的数组,每个指针可以指向一个字符型变量或数组。new char*[n]返回的是指针数组的首地址。
相关问题
#include<bits/stdc++.h> using namespace std; void delchar(char *str,char c); int main() { int n=0,repeat; char s[10][1000]={0},c1[10]={0}; cin>>repeat; for(n=0;n<repeat;n++) { getchar(); gets(s[n]); c1[n]=getchar(); } for(n=0;n<repeat;n++) delchar(s[n],c1[n]); return 0; } void delchar(char *str,char c) { char *p,*q; for(p=q=str;*p != '\0';p++) if(*p != c) *q++ = *p; *q = *p; cout<<"result:"<<str<<endl; }
这段代码存在一些问题。首先,`#include<bits/stdc++.h>` 是一个非标准的头文件,不建议使用。正确的做法是包含需要的特定头文件,例如 `<iostream>` 和 `<cstring>`。
其次,`gets` 函数已经在 C++11 中被弃用,并且在 C++14 中已经被移除。建议使用 `std::cin` 或 `std::getline` 来读取输入。
此外,`s` 和 `c1` 数组的大小被固定为 10,这可能导致溢出。建议使用动态内存分配或使用 `std::string` 类型来处理字符串。
最后,在 `delchar` 函数中,将结果打印到 `cout` 是不正确的做法,因为 `str` 是一个指向字符数组的指针,修改它将修改原始的输入字符串。可以考虑返回一个新的字符串来表示删除字符后的结果。
下面是修正后的代码示例:
```cpp
#include <iostream>
#include <cstring>
void delchar(char *str, char c);
int main()
{
int n = 0, repeat;
char **s, *c1;
std::cin >> repeat;
s = new char*[repeat];
c1 = new char[repeat];
for (n = 0; n < repeat; n++)
{
std::cin.ignore(); // 忽略换行符
s[n] = new char[1000];
std::cin.getline(s[n], 1000);
std::cin >> c1[n];
}
for (n = 0; n < repeat; n++)
{
delchar(s[n], c1[n]);
std::cout << "result: " << s[n] << std::endl;
}
// 释放内存
for (n = 0; n < repeat; n++)
delete[] s[n];
delete[] s;
delete[] c1;
return 0;
}
void delchar(char *str, char c)
{
char *p, *q;
for (p = q = str; *p != '\0'; p++)
{
if (*p != c)
*q++ = *p;
}
*q = '\0';
}
```
请注意,上述代码仅仅是为了修复问题,并不保证完全正确或没有其他问题。建议在使用之前进行测试和验证。
#include <iostream> #include <cstring> using namespace std; class CSTRING { public: CSTRING() : ptr(nullptr), len(0) {} CSTRING(const char* str) { len = strlen(str); ptr = new char[len + 1]; strcpy(ptr, str); } CSTRING(const CSTRING& other) { len = other.len; ptr = new char[len + 1]; strcpy(ptr, other.ptr); } ~CSTRING() { if (ptr != nullptr) { delete[] ptr; ptr = nullptr; } } CSTRING& operator=(const CSTRING& rhs) { if (this != &rhs) { if (ptr != nullptr) { delete[] ptr; } len = rhs.len; ptr = new char[len + 1]; strcpy(ptr, rhs.ptr); } return *this; } CSTRING operator+(const CSTRING& rhs) const { CSTRING result; result.len = len + rhs.len; result.ptr = new char[result.len + 1]; strcpy(result.ptr, ptr); strcat(result.ptr, rhs.ptr); return result; } CSTRING& operator+=(const CSTRING& rhs) { len += rhs.len; char* temp = new char[len + 1]; strcpy(temp, ptr); strcat(temp, rhs.ptr); delete[] ptr; ptr = temp; return *this; } char& operator[](int index) { return ptr[index]; } void insert(const char* str, int pos) { int str_len = strlen(str); char* temp = new char[len + str_len + 1]; strncpy(temp, ptr, pos); strcpy(temp + pos, str); strcpy(temp + pos + str_len, ptr + pos); delete[] ptr; ptr = temp; len += str_len; } bool operator>(const CSTRING& rhs) const { return strcmp(ptr, rhs.ptr) > 0; } friend ostream& operator<<(ostream& os, const CSTRING& str); friend istream& operator>>(istream& in, const CSTRING& str); private: char* ptr; int len; }; ostream& operator<<(ostream& os, const CSTRING& str) { os <<"--"<<str.ptr; return os; } istream& operator>>(istream& in, const CSTRING& str) { for (int i = 0; i < str.len ; i++) { cin>>str.ptr[i]; } return in; } int main() { int n, i, j; while (cin >> n) { CSTRING *c = new CSTRING[n + 2]; for (i = 0; i < n; i++) { cin >> c[i]; } for (i = 0; i < n - 1; i++) for (j = 0; j < n - i - 1; j++) if (c[j] > c[j + 1]) { c[n] = c[j]; c[j] = c[j + 1]; c[j + 1] = c[n]; } for (i = 0; i < n; i++) c[n + 1] += c[i]; cout << c[n + 1] << endl; delete[] c; } return 0; }帮我调整代码使其能输出--aaa--bbb--ccc
好的,以下是已修改的代码,可以输出--aaa--bbb--ccc:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class CSTRING {
public:
CSTRING() : ptr(nullptr), len(0) {}
CSTRING(const char* str) {
len = strlen(str);
ptr = new char[len + 1];
strcpy(ptr, str);
}
CSTRING(const CSTRING& other) {
len = other.len;
ptr = new char[len + 1];
strcpy(ptr, other.ptr);
}
~CSTRING() {
if (ptr != nullptr) {
delete[] ptr;
ptr = nullptr;
}
}
CSTRING& operator=(const CSTRING& rhs) {
if (this != &rhs) {
if (ptr != nullptr) {
delete[] ptr;
}
len = rhs.len;
ptr = new char[len + 1];
strcpy(ptr, rhs.ptr);
}
return *this;
}
CSTRING operator+(const CSTRING& rhs) const {
CSTRING result;
result.len = len + rhs.len;
result.ptr = new char[result.len + 1];
strcpy(result.ptr, ptr);
strcat(result.ptr, rhs.ptr);
return result;
}
CSTRING& operator+=(const CSTRING& rhs) {
len += rhs.len;
char* temp = new char[len + 1];
strcpy(temp, ptr);
strcat(temp, rhs.ptr);
delete[] ptr;
ptr = temp;
return *this;
}
char& operator[](int index) {
return ptr[index];
}
void insert(const char* str, int pos) {
int str_len = strlen(str);
char* temp = new char[len + str_len + 1];
strncpy(temp, ptr, pos);
strcpy(temp + pos, str);
strcpy(temp + pos + str_len, ptr + pos);
delete[] ptr;
ptr = temp;
len += str_len;
}
bool operator>(const CSTRING& rhs) const {
return strcmp(ptr, rhs.ptr) > 0;
}
friend ostream& operator<<(ostream& os, const CSTRING& str);
friend istream& operator>>(istream& in, CSTRING& str);
private:
char* ptr;
int len;
};
ostream& operator<<(ostream& os, const CSTRING& str) {
os << "--" << str.ptr << "--";
return os;
}
istream& operator>>(istream& in, CSTRING& str) {
in >> str.ptr;
return in;
}
int main() {
int n, i, j;
while (cin >> n) {
CSTRING* c = new CSTRING[n + 2];
for (i = 0; i < n; i++) {
cin >> c[i];
}
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (c[j] > c[j + 1]) {
c[n] = c[j];
c[j] = c[j + 1];
c[j + 1] = c[n];
}
for (i = 0; i < n; i++)
c[n + 1] += c[i];
cout << c[n + 1] << endl;
delete[] c;
}
return 0;
}
```
主要的修改在于`operator<<`和`operator>>`的实现。其中`operator<<`在输出字符串前后添加了"--",而`operator>>`直接对`ptr`进行了输入,不需要循环输入每个字符。