質問編集履歴

2

コードの改善

2020/03/08 22:17

投稿

do_Shiro_to
do_Shiro_to

スコア15

test CHANGED
File without changes
test CHANGED
@@ -126,7 +126,7 @@
126
126
 
127
127
  int mCurrentIndex = 0;
128
128
 
129
- Vector<T> *mMyVector = nullptr;
129
+ Vector<T> *mMyVector = nullptr;    
130
130
 
131
131
  friend class Vector<T>;
132
132
 
@@ -144,17 +144,15 @@
144
144
 
145
145
  public:
146
146
 
147
-
148
-
149
147
  T& GetData()
150
148
 
151
149
  {
152
150
 
153
- if (mData[mCurrentIndex] != nullptr)
151
+ if (mMyVector[mCurrentIndex] != nullptr)
154
152
 
155
153
  {
156
154
 
157
- return mData[mCurrentIndex];
155
+ return mMyVector[mCurrentIndex];
158
156
 
159
157
  }
160
158
 
@@ -166,7 +164,7 @@
166
164
 
167
165
  {
168
166
 
169
- mCurrentIndex = mCurrentIndex + 1;
167
+ mCurrentIndex += 1;
170
168
 
171
169
  }
172
170
 
@@ -178,13 +176,15 @@
178
176
 
179
177
  mCurrentIndex = 0;
180
178
 
179
+ int tSize = mMyVector->Size();
180
+
181
181
 
182
182
 
183
- while (Size() != mCurrentIndex-1)
183
+ while (tSize != mCurrentIndex-1)
184
184
 
185
185
  {
186
186
 
187
- if (mData[mCurrentIndex] == rhs.GetData)
187
+ if (mMyVector[mCurrentIndex] == rhs.GetData)
188
188
 
189
189
  {
190
190
 
@@ -198,9 +198,7 @@
198
198
 
199
199
 
200
200
 
201
-
202
-
203
- if (tCount == Size())
201
+ if (tCount == tSize)
204
202
 
205
203
  {
206
204
 
@@ -218,13 +216,11 @@
218
216
 
219
217
  }
220
218
 
221
- }
222
-
223
219
  };
224
220
 
225
221
 
226
222
 
227
- Iterator Insert(Iterator tWhere, const T& tWhat) // These two use iterators now
223
+ Iterator Insert(Iterator tWhere, const T& tWhat)
228
224
 
229
225
  {
230
226
 
@@ -236,6 +232,8 @@
236
232
 
237
233
  int tSize = 0;
238
234
 
235
+ int tRet = 0;
236
+
239
237
  T* tData = new T[mCapacity];
240
238
 
241
239
 
@@ -244,7 +242,7 @@
244
242
 
245
243
  {
246
244
 
247
- if(i == tWhere)
245
+ if(mData[i] == tWhere)
248
246
 
249
247
  {
250
248
 
@@ -252,6 +250,8 @@
252
250
 
253
251
  tSize++;
254
252
 
253
+ tRet = i + 1;
254
+
255
255
  }
256
256
 
257
257
  tData[tSize] = mData[i];
@@ -270,55 +270,311 @@
270
270
 
271
271
 
272
272
 
273
+ Iterator tAnswer(mData, tRet);
274
+
275
+ return tAnswer;
276
+
277
+ }
278
+
279
+ Iterator Erase(Iterator tWhere)
280
+
281
+ {
282
+
283
+ int tSize = 0;
284
+
285
+ int tRet = 0;
286
+
287
+ T* tData = new T[mCapacity];
288
+
289
+
290
+
291
+ for (int i = 0; i < mSize; i++)
292
+
293
+ {
294
+
295
+ if (mData[i] == tWhere)
296
+
297
+ {
298
+
299
+ ++tSize;
300
+
301
+ tData[i] = mData[tSize];
302
+
303
+ tRet = i+1;
304
+
305
+ }
306
+
307
+ tData[i] = mData[tSize];
308
+
309
+ ++tSize;
310
+
311
+ }
312
+
313
+
314
+
315
+ delete[] mData;
316
+
317
+ mData = tData;
318
+
319
+ mSize = i;
320
+
321
+
322
+
323
+ Iterator tAnswer(mData, tRet);
324
+
325
+ return tAnswer;
326
+
327
+ }
328
+
329
+ Iterator Begin()
330
+
331
+ {
332
+
333
+ Iterator tAnswer(mData, 0);
334
+
335
+ return tAnswer;
336
+
337
+ }
338
+
339
+ Iterator End()
340
+
341
+ {
342
+
343
+ Iterator tAnswer(mData, mSize);
344
+
345
+ return tAnswer;
346
+
347
+ }
348
+
349
+ };
350
+
351
+
352
+
353
+ template<typename T>
354
+
355
+ T Vector<T>::sUndefined;
356
+
357
+ ```
358
+
359
+
360
+
361
+ ```C++
362
+
363
+ template <typename T>
364
+
365
+ class List
366
+
367
+ {
368
+
369
+ struct ListNode
370
+
371
+ {
372
+
373
+ ListNode()
374
+
375
+ {
376
+
377
+ mPrev = nullptr;
378
+
379
+ mNext = nullptr;
380
+
381
+ }
382
+
383
+ T mData;
384
+
385
+ ListNode* mPrev;
386
+
387
+ ListNode* mNext;
388
+
389
+ };
390
+
391
+
392
+
393
+ ListNode* mHead;
394
+
395
+ ListNode* mTail;
396
+
397
+
398
+
399
+ public:
400
+
401
+ List()
402
+
403
+ {
404
+
405
+ //mHead = nullptr;// Technically works, but we gain so much with sentinel nodes
406
+
407
+ //mTail = nullptr;
408
+
409
+
410
+
411
+ mHead = new ListNode;// You always know there is a node to the left and right
412
+
413
+ mTail = new ListNode;
414
+
415
+ mHead->mNext = mTail;
416
+
417
+ mTail->mPrev = mHead;
418
+
419
+ }
420
+
421
+ List(const List& tOther) : List();
422
+
423
+
424
+
425
+ List& operator = (const List& tRHS);
426
+
427
+
428
+
429
+ ~List();
430
+
431
+
432
+
433
+ void PushFront(const T& tWhat);
434
+
435
+
436
+
437
+ void PopFront();
438
+
439
+
440
+
441
+ static T sUnspecifiedStaticVar;
442
+
443
+
444
+
445
+ T& Front();
446
+
447
+
448
+
449
+ void PushBack(const T& tWhat);
450
+
451
+
452
+
453
+ void PopBack();
454
+
455
+
456
+
457
+ T& Back();
458
+
459
+
460
+
461
+ int Size();
462
+
463
+
464
+
465
+ void Clear();
466
+
467
+
468
+
469
+ T& At(int tWhere) const;
470
+
471
+
472
+
473
+ class Iterator
474
+
475
+ {
476
+
477
+ ListNode* mCurrent;
478
+
479
+ friend class List<T>;
480
+
481
+ Iterator(ListNode* tStart)
482
+
483
+ {
484
+
485
+ mCurrent = tStart;
486
+
487
+ }
488
+
489
+ public:
490
+
491
+ T& GetData()
492
+
493
+ {
494
+
495
+ return mCurrent->mData;
496
+
497
+ }
498
+
499
+ void Next()
500
+
501
+ {
502
+
503
+ mCurrent = mCurrent->mNext;
504
+
505
+ }
506
+
507
+ bool IsEqual(const Iterator& rhs) const
508
+
509
+ {
510
+
511
+ return mCurrent == rhs.mCurrent;
512
+
513
+ }
514
+
515
+ };
516
+
517
+
518
+
519
+ Iterator Insert(Iterator tWhere, const T& tWhat)
520
+
521
+ {
522
+
523
+ ListNode* tNew = new ListNode;
524
+
525
+ tNew->mData = tWhat;
526
+
273
527
 
274
528
 
529
+ ListNode* tPrev = tWhere;
530
+
531
+ ListNode* tNext = tPrev->mNext;
532
+
533
+ tNext->mPrev = tPrev;
534
+
535
+
536
+
537
+ tPrev->mNext = tNew;
538
+
539
+ tNew->mPrev = tPrev;
540
+
541
+ tNew->mNext = tNext;
542
+
543
+ tNext->mPrev = tNew;
544
+
545
+
546
+
275
- Iterator tAnswer(mMyVector, tWhere);
547
+ return Iterator(tNew->mNext);
276
-
277
- return tAnswer;
548
+
278
-
279
- }
549
+ }
280
-
550
+
281
- Iterator Erase(Iterator tWhere)
551
+ Iterator Erase(Iterator tWhat)
282
-
552
+
283
- {
553
+ {
284
-
554
+
285
- int tSize = 0;
555
+ ListNode* tDead = tWhat;
286
-
287
- T* tData = new T[mCapacity];
556
+
288
-
289
-
290
-
291
- for (int i = 0; i < mSize; i++)
292
-
293
- {
294
-
295
- if (i == tWhere)
296
-
297
- {
298
-
299
- ++tSize;
300
-
301
- tData[i] = mData[tSize];
557
+ ListNode* tNext = tDead->mNext;
302
-
303
- }
558
+
304
-
305
- tData[i] = mData[tSize];
559
+ ListNode* tPrev = tDead->mPrev;
306
-
560
+
307
- ++tSize;
561
+ tPre->mNext = tDead;
308
-
562
+
309
- }
563
+ tNext->mPrev = tDead;
310
-
564
+
311
- delete[] mData;
565
+ delete tDead;
312
-
313
- mData = tData;
566
+
314
-
315
- mSize = i;
567
+
316
-
317
-
318
-
568
+
319
- Iterator tAnswer(mMyVector, tWhere+1);
569
+ tPrev->mNext = tNext;
570
+
320
-
571
+ tNext->mPrev = tPrev;
572
+
573
+
574
+
321
- return tAnswer;
575
+ return Iterator(tNext);
576
+
577
+
322
578
 
323
579
  }
324
580
 
@@ -326,9 +582,7 @@
326
582
 
327
583
  {
328
584
 
329
- Iterator tAnswer(mMyVector, 0);
585
+ return Iterator(mHead->mNext);
330
-
331
- return tAnswer;
332
586
 
333
587
  }
334
588
 
@@ -336,256 +590,98 @@
336
590
 
337
591
  {
338
592
 
339
- Iterator tAnswer(mMyVector, mSize-1);
340
-
341
- return tAnswer;
593
+ return Iterator(mTail);
342
594
 
343
595
  }
344
596
 
345
597
  };
346
598
 
347
-
348
-
349
- template<typename T>
350
-
351
- T Vector<T>::sUndefined;
352
-
353
599
  ```
354
600
 
355
601
 
356
602
 
603
+ VectorとListの関数を簡略しているので、このままコンパイルはできません。
604
+
605
+ こんな感じのmainで動かせればいいなと思います。
606
+
357
607
  ```C++
358
608
 
359
- template <typename T>
609
+ #include <iostream>
610
+
360
-
611
+ #include "List.h"
612
+
613
+ #include "Vector.h"
614
+
615
+
616
+
617
+ using namespace std;
618
+
619
+
620
+
361
- class List
621
+ int main()
362
622
 
363
623
  {
364
624
 
365
- struct ListNode
366
-
367
- {
368
-
369
- ListNode()
370
-
371
- {
372
-
373
- mPrev = nullptr;
374
-
375
- mNext = nullptr;
376
-
377
- }
378
-
379
- T mData;
380
-
381
- ListNode* mPrev;
382
-
383
- ListNode* mNext;
384
-
385
- };
386
-
387
-
388
-
389
- ListNode* mHead;
390
-
391
- ListNode* mTail;
392
-
393
-
394
-
395
- public:
396
-
397
- List()
398
-
399
- {
400
-
401
- //mHead = nullptr;// Technically works, but we gain so much with sentinel nodes
402
-
403
- //mTail = nullptr;
404
-
405
-
406
-
407
- mHead = new ListNode;// You always know there is a node to the left and right
408
-
409
- mTail = new ListNode;
410
-
411
- mHead->mNext = mTail;
412
-
413
- mTail->mPrev = mHead;
414
-
415
- }
416
-
417
- List(const List& tOther) : List();
418
-
419
-
420
-
421
- List& operator = (const List& tRHS);
422
-
423
-
424
-
425
- ~List();
426
-
427
-
428
-
429
- void PushFront(const T& tWhat);
430
-
431
-
432
-
433
- void PopFront();
434
-
435
-
436
-
437
- static T sUnspecifiedStaticVar;
438
-
439
-
440
-
441
- T& Front();
442
-
443
-
444
-
445
- void PushBack(const T& tWhat);
446
-
447
-
448
-
449
- void PopBack();
450
-
451
-
452
-
453
- T& Back();
454
-
455
-
456
-
457
- int Size();
458
-
459
-
460
-
461
- void Clear();
462
-
463
-
464
-
465
- T& At(int tWhere) const;
466
-
467
-
468
-
469
- class Iterator
470
-
471
- {
472
-
473
- ListNode* mCurrent;
474
-
475
- friend class List<T>;
476
-
477
- Iterator(ListNode* tStart)
478
-
479
- {
480
-
481
- mCurrent = tStart;
482
-
483
- }
484
-
485
- public:
486
-
487
- T& GetData()
488
-
489
- {
490
-
491
- return mCurrent->mData;
492
-
493
- }
494
-
495
- void Next()
496
-
497
- {
498
-
499
- mCurrent = mCurrent->mNext;
500
-
501
- }
502
-
503
- bool IsEqual(const Iterator& rhs) const
504
-
505
- {
506
-
507
- return mCurrent == rhs.mCurrent;
508
-
509
- }
510
-
511
- };
512
-
513
-
514
-
515
- Iterator Insert(Iterator tWhere, const T& tWhat)
516
-
517
- {
518
-
519
- ListNode* tNew = new ListNode;
520
-
521
- ListNode* tPrev = tWhere->mPrev;
522
-
523
- ListNode* tNext = tWhere->mNext;
524
-
525
- tNew->mData = tWhat;
526
-
527
-
528
-
529
- tPrev->mNext = tNew;
530
-
531
- tNew->mPrev = tPrev;
532
-
533
- tNew->mNext = tNext;
534
-
535
- tNext->mPrev = tNew;
536
-
537
-
538
-
539
- return Iterator(tNew);
540
-
541
- }
542
-
543
- Iterator Erase(Iterator tWhat)
544
-
545
- {
546
-
547
- ListNode* tPrev = tWhat->mPrev;
548
-
549
- ListNode* tNext = tWhat->mNext;
550
-
551
- delete tWhat;
552
-
553
-
554
-
555
- tPrev->mNext = tNext;
556
-
557
- tNext->mPrev = tPrev;
558
-
559
-
560
-
561
- return Iterator(tNext);
562
-
563
-
564
-
565
- }
566
-
567
- Iterator Begin()
568
-
569
- {
570
-
571
- return Iterator(mHead->mNext);
572
-
573
- }
574
-
575
- Iterator End()
576
-
577
- {
578
-
579
- return Iterator(mTail);
580
-
581
- }
582
-
583
- };
625
+ Vector<int> tTester;
626
+
627
+ tTester.PushBack(0);
628
+
629
+ tTester.PushBack(1);
630
+
631
+ tTester.PushBack(2);
632
+
633
+
634
+
635
+ for (Vector<int>::Iterator iter = tTester.Begin(); !iter.IsEqual(tTester.End()); iter.Next())
636
+
637
+ {
638
+
639
+ cout << iter.GetData() << " ";
640
+
641
+ }
642
+
643
+
644
+
645
+ cout << endl;
646
+
647
+ tTester.Erase(tTester.Begin());
648
+
649
+
650
+
651
+ for (Vector<int>::Iterator iter = tTester.Begin(); !iter.IsEqual(tTester.End()); iter.Next())
652
+
653
+ {
654
+
655
+ cout << iter.GetData() << " ";
656
+
657
+ }
658
+
659
+
660
+
661
+ cout << endl;
662
+
663
+ tTester.Insert(tTester.Begin(), 999);
664
+
665
+ tTester.Insert(tTester.Begin(), 888);
666
+
667
+
668
+
669
+ for (Vector<int>::Iterator iter = tTester.Begin(); !iter.IsEqual(tTester.End()); iter.Next())
670
+
671
+ {
672
+
673
+ cout << iter.GetData() << " ";
674
+
675
+ }
676
+
677
+ }
584
678
 
585
679
  ```
586
680
 
587
681
 
588
682
 
683
+
684
+
589
685
  ### 補足情報(FW/ツールのバージョンなど)
590
686
 
591
687
 

1

脱字の修正

2020/03/08 22:17

投稿

do_Shiro_to
do_Shiro_to

スコア15

test CHANGED
File without changes
test CHANGED
@@ -308,6 +308,12 @@
308
308
 
309
309
  }
310
310
 
311
+ delete[] mData;
312
+
313
+ mData = tData;
314
+
315
+ mSize = i;
316
+
311
317
 
312
318
 
313
319
  Iterator tAnswer(mMyVector, tWhere+1);