Нужно чтобы метод pop(); удалял элемент массива ptr по индексу, я пытался это сделать классическим способом, но код крушиться. Есть ли легальный способ удалить элемент из массива по индексу ?
void pop(int z) {
for (int i = z; i < x; i++) {
ptr[i] = ptr[i + 1];
}
x = x - 1;
}
выше сам метод pop(); а вот и сам код
#include <iostream>
using namespace std;
#include <string>
class PC {
public:
int i_one = 0;
int i_t=0;
int i_h=0;
int i_p=0;
int i_l=0;
int i_m=0;
int i_n=0;
int i_b=0;
int x =16;
string* ptr = new string[x];
void print() {
for (int i = 0; i < x; i++) {
cout << ptr[i] <<" and NOMER: "<<i<< endl;
}
}
void other_add() {
//int i_one = 0;
cout << "enter marka PC:" << "; " << endl;
cin >> mark_pk;
ptr[0 + i_one] = mark_pk;
i_one += 8;
}
void other_add_1() {
// int i_t = 0;
cout << "enter type processor:" << "; " << endl;
cin >> type_proc;
ptr[1 + i_t] = type_proc;
i_t += 8;
}
void other_add_2() {
// int i_h = 0;
cout << " tact chastota procesora:" << endl;
cin >> takt;
ptr[2 + i_h] = takt;
i_h += 8;
}
void other_add_3() {
// int i_p = 0;
cout <<" obem memory:" << endl;
cin >> ob_memor;
ptr[3 + i_p] = ob_memor;
i_p += 8;
}
void other_add_4() {
// int i_l = 0;
cout << " obem gorstkoho disc: " << endl;
cin >> ob_gr_disk;
ptr[4 + i_l] = ob_gr_disk;
i_l += 8;
}
void other_add_5() {
// int i_m = 0;
cout << "obem video card:" << endl;
cin >> ob_memor_video;
ptr[5 + i_m] = ob_memor_video;
i_m += 8;
}
void other_add_6() {
// int i_n = 0;
cout << "value PC:" << value_pc << endl;
cin >> value_pc;
ptr[6 + i_n] = value_pc;
i_n += 8;
}
void other_add_7() {
// int i_b = 0;
cout << " kilk in nayavnosti" << endl;
cin >> kilkist;
ptr[7 + i_b] = kilkist;
i_b += 8;
}
void add() {
other_add();
other_add_1();
other_add_2();
other_add_3();
other_add_4();
other_add_5();
other_add_6();
other_add_7();
}
string nazva_pc;
string mark_pk;
string type_proc;
string takt;
string ob_memor;
string ob_gr_disk;
string ob_memor_video;
string value_pc;
string kilkist;
void pop(int z) {
for (int i = z; i < x; i++) {
ptr[i] = ptr[i + 1];
}
x = x - 1;
}
};
int main()
{
PC *s=new PC;
s->add();
s->add();
cout << "fffffffffffffffffffffffffff" << endl;
s->pop(5);
s->print();
}
for (int i = z; i < x; i++) { ptr[i] = ptr[i + 1];
- что будет приi==x-1
? Обращение кptr[x]
за пределами массива. UB... – Harry 31 мая '20 в 18:49