Форум программистов, компьютерный форум, киберфорум
С++ для начинающих
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 4.50/4: Рейтинг темы: голосов - 4, средняя оценка - 4.50
2 / 2 / 1
Регистрация: 23.06.2014
Сообщений: 110
1

Не получается удалить элементы из первого вектора, которые есть во втором

27.01.2015, 23:38. Показов 644. Ответов 2
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
Нужно перегрузить оператор - что-бы удаляло из первого вектора елементы которые есть во втором
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
 
using namespace std;
 
typedef unsigned int uint;
 
template <class T>
 
 
class vector
{
    T *data;
    uint count;
 
public: vector() : data(0), count(0){}
 
public:
        explicit vector(uint initcount)
        {
            if (!initcount){
                data = new T[initcount];
                count = initcount;
            }
 
        }
 
        ~vector(){ clear(); }
 
        void clear()
        {
            delete[]data;
            count = 0;
        }
 
        uint getcount() { return count; }
        bool isempty(){ return count == 0; }
        bool operator ! () { isempty(); }
 
        void add(const T& item)
        {
            if (!count)
            {
                data = new T;
                *data = item;
            }
            else
            {
                T *temp = new T[count + 1];
                for (int i = 0; i < count; i++)
                {
                    temp[i] = data[i];
                }
                temp[count] = item;
                delete[] data;
                data = temp;
            }
            count++;
        }
 
        void remove(const T& index)
        {
            if (index > count)
            {
                return;
            }
 
            if (count == 1)
            {
                delete[] data;
            }
 
            else
            {
                T* temp = new T[count - 1];
 
                for (int i = 0; i < index; i++)
                {
                    if (i < index)
                    {
                        temp[i] = data[i];
                    }
 
                    else if (i > index)
                    {
                        temp[i - 1] = data[i];
                    }
                    delete[] data;
                    data = temp;
 
                }
 
            }
            count--;
        }
 
        T& operator [](uint index)
        {
            if (index >= count)
            {
                return data[0];
            }
 
            return data[index];
        }
 
        vector& operator +(vector &v)
        {
            for (int i = 0; i < v.getcount(); i++)
            {
                add(v[i]);
            }
            return *this;
 
        }
 
        vector& operator -(vector &v)
        {
 
            for (int i = 0; i < v.getcount(); i++)
                for (int j = 0; j < this->getcount(); j++)
                {
                if (v.data[i] == data[j])
                {
                    this->remove(i);
                    this->print();
                    v.print();
                }
 
                cout << endl;
                }
            return *this;
        }
        
 
    /*  void operator >> (const T&item)
        {
            if (!count)
            {
                data = new T;
                *data = item;
            }
            T *temp = new T[count + 1];
            for (int i = 0; i < count; i++)
            {
                temp[i] = data[i];
            }
 
            temp[count + 1] = item;
            delete[] data;
            data = temp;
            count++;
        //  return 0;
        }
        */
 
        void insert(uint index, const T& item)
        {
 
            if (index >= count)
            {
                return;
            }
 
            if (!count)
            {
                data = new T;
                *data = item;
            }
 
            else
            {
                T* temp = new T[count + 1];
 
                for (uint i = 0; i < count; i++)
                {
                    if (i < index)
                    {
                        temp[i] = data[i];
                    }
 
                    else if (i >= index)
                    {
                        temp[i + 1] = data[i];
                    }
                }
                temp[index] = item;
 
                delete[] data;
                data = temp;
            }
            count++;
        }
 
        void print()
        {
            for (int i = 0; i < this->getcount(); i++)
            {
                cout << data[i] << endl;
            }
            cout << endl;
        }
        
 
};
 
 
int main(int argc, _TCHAR* argv[])
{
 
    vector<int> vec;
    vector<int> vec1;
    vec.add(5);
    vec.add(0);
    vec.add(7);
    vec.add(3);
    vec.add(10);
 
    vec1.add(10);
    vec1.add(5);
    vec1.add(3);
    vec1.add(0);
    vec1.add(7);
    
    vec.print();
    vec1.print();
    //cout << vec.getcount() << endl;
    //vec + vec1;
    cout << vec.getcount() << endl;
    vec - vec1;
 
 
    //cout << vec1.getcount() << endl;
    //vec - vec1;
    cout << vec.getcount() << endl;
 
 
 
    _getch();
}
Не получается удалить элементы из первого вектора, которые есть во втором


на дебаге после удаления петерки вектор заполняеться мусором а 5 остается
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
27.01.2015, 23:38
Ответы с готовыми решениями:

Заменить нулями те элементы первого массива, которые есть во втором
Даны два массива действительных чисел по 12 элементов в каждом. Заменить нулями те элементы первого...

Удалить из одного массива элементы которые есть во втором
Нужно удалить из одного массива элементы которые есть во втором Этот код почему-то не работает ...

Переставить элементы первого списка, чтобы сначала шли элементы, которые встречаются во втором списке
1) переставить елементы 1го списка таким образом, что бы сначала шли те элементы, что встречаются...

Удалить из первого слова буквы, которые встречаются во втором слове
Даны по одному слову в двух edit, Нужно удалить из первого слова буквы которые встречаются во...

2
2 / 2 / 1
Регистрация: 23.06.2014
Сообщений: 110
28.01.2015, 00:05  [ТС] 2
чучуть переделал новые ошибки
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
 
using namespace std;
 
typedef unsigned int uint;
 
template <class T>
 
 
class vector
{
    T *data;
    uint count;
 
public: vector() : data(0), count(0){}
 
public:
        explicit vector(uint initcount)
        {
            if (!initcount){
                data = new T[initcount];
                count = initcount;
            }
 
        }
 
        ~vector(){ clear(); }
 
        void clear()
        {
            delete[]data;
            count = 0;
        }
 
        uint getcount() { return count; }
        bool isempty(){ return count == 0; }
        bool operator ! () { isempty(); }
 
        void add(const T& item)
        {
            if (!count)
            {
                data = new T;
                *data = item;
            }
            else
            {
                T *temp = new T[count + 1];
                for (int i = 0; i < count; i++)
                {
                    temp[i] = data[i];
                }
                temp[count] = item;
                delete[] data;
                data = temp;
            }
            count++;
        }
 
        void remove(const T& index)
        {
            if (index > count)
            {
                return;
            }
 
            if (count == 1)
            {
                delete[] data;
            }
 
            else
            {
                T* temp = new T[count - 1];
 
                for (int i = 0; i < index; i++)
                {
                    if (i < index)
                    {
                        temp[i] = data[i];
                    }
 
                    else if (i > index)
                    {
                        temp[i - 1] = data[i];
                    }
                    
 
                }
                delete[] data;
                data = temp;
            }
            
            count--;
        }
 
        T& operator [](uint index)
        {
            if (index >= count)
            {
                return data[0];
            }
 
            return data[index];
        }
 
        vector& operator +(vector &v)
        {
            for (int i = 0; i < v.getcount(); i++)
            {
                add(v[i]);
            }
            return *this;
 
        }
 
        vector& operator -(vector &v)
        {
 
            for (int i = 0; i < v.getcount(); i++)
                for (int j = 0; j < this->getcount(); j++)
                {
                if (v.data[i] == data[j])
                {
                    this->remove(j);
                    this->print();
                    v.print();
                }
 
                cout << endl;
                }
            return *this;
        }
        
 
    /*  void operator >> (const T&item)
        {
            if (!count)
            {
                data = new T;
                *data = item;
            }
            T *temp = new T[count + 1];
            for (int i = 0; i < count; i++)
            {
                temp[i] = data[i];
            }
 
            temp[count + 1] = item;
            delete[] data;
            data = temp;
            count++;
        //  return 0;
        }
        */
 
        void insert(uint index, const T& item)
        {
 
            if (index >= count)
            {
                return;
            }
 
            if (!count)
            {
                data = new T;
                *data = item;
            }
 
            else
            {
                T* temp = new T[count + 1];
 
                for (uint i = 0; i < count; i++)
                {
                    if (i < index)
                    {
                        temp[i] = data[i];
                    }
 
                    else if (i >= index)
                    {
                        temp[i + 1] = data[i];
                    }
                }
                temp[index] = item;
 
                delete[] data;
                data = temp;
            }
            count++;
        }
 
        void print()
        {
            for (int i = 0; i < this->getcount(); i++)
            {
                cout << data[i] << endl;
            }
            cout << endl;
        }
        
 
};
 
 
int main(int argc, _TCHAR* argv[])
{
 
    vector<int> vec;
    vector<int> vec1;
    vec.add(5);
    vec.add(0);
    vec.add(7);
    vec.add(3);
    vec.add(10);
 
    vec1.add(10);
    vec1.add(5);
    vec1.add(3);
    vec1.add(0);
    vec1.add(7);
    
    vec.print();
    vec1.print();
    //cout << vec.getcount() << endl;
    //vec + vec1;
    cout << vec.getcount() << endl;
    vec - vec1;
 
 
    //cout << vec1.getcount() << endl;
    //vec - vec1;
    cout << vec.getcount() << endl;
 
 
 
    _getch();
}
0
21 / 21 / 6
Регистрация: 09.01.2014
Сообщений: 118
02.02.2015, 10:24 3
Вам нужно в ветку по С++, там Вам быстрее ответят.
0
02.02.2015, 10:24
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
02.02.2015, 10:24
Помогаю со студенческими работами здесь

Как удалить из первого массива те значения, которые имеются во втором массиве?
Как удалить из первого массива те значения, которые имеются во втором массиве?

Из первого слова удалить все буквы, которые встречаются во втором слове
Даны два слова. Из первого слова удалить все буквы, которые встречаются во втором слове. Помогите...

Создать новый стек из тех символов первого стека которые есть во втором
Из двух заданных стеков, хранящих символы создать новый стек из тех символов первого стека которые...

Удалить из первого списка те элементы, что отсутствуют во втором
Удалить из первого списка те элементы, что отсутствуют во втором.


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
3
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru