1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

古いC++コードをモダンC++に修正(問題編)

Last updated at Posted at 2025-09-01

問題編

1:動的配列とループ
#include <iostream>

int main() {
    int* arr = new int[5];
    for (int i = 0; i < 5; ++i) {
        arr[i] = i * i;
    }

    for (int i = 0; i < 5; ++i) {
        std::cout << arr[i] << std::endl;
    }

    delete[] arr;
}
2:文字列の配列
#include <iostream>
#include <cstring>

int main() {
    char names[3][10];
    strcpy(names[0], "Alice");
    strcpy(names[1], "Bob");
    strcpy(names[2], "Carol");

    for (int i = 0; i < 3; ++i) {
        std::cout << names[i] << std::endl;
    }
}
3:構造体配列の初期化と出力
#include <iostream>

struct Point {
    int x;
    int y;
};

int main() {
    Point* points = new Point[3];
    points[0].x = 1; points[0].y = 2;
    points[1].x = 3; points[1].y = 4;
    points[2].x = 5; points[2].y = 6;

    for (int i = 0; i < 3; ++i) {
        std::cout << points[i].x << ", " << points[i].y << std::endl;
    }

    delete[] points;
}
4:ポインタの多重管理
#include <iostream>

int main() {
    int* a = new int(10);
    int* b = new int(20);

    int sum = *a + *b;
    std::cout << sum << std::endl;

    delete a;
    delete b;
}
5:整数配列の逆順出力
#include <iostream>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};

    for (int i = 4; i >= 0; --i) {
        std::cout << arr[i] << std::endl;
    }
}
6:文字列の動的配列
#include <iostream>
#include <cstring>

int main() {
    char* names[3];
    names[0] = new char[10];
    names[1] = new char[10];
    names[2] = new char[10];

    strcpy(names[0], "Tom");
    strcpy(names[1], "Bob");
    strcpy(names[2], "Alice");

    for (int i = 0; i < 3; ++i) {
        std::cout << names[i] << std::endl;
    }

    for (int i = 0; i < 3; ++i) delete[] names[i];
}
7:構造体のコピーと参照
#include <iostream>

struct Point {
    int x, y;
};

int main() {
    Point p1 = {1,2};
    Point p2 = p1; // コピー
    p2.x = 10;

    std::cout << p1.x << ", " << p1.y << std::endl;
    std::cout << p2.x << ", " << p2.y << std::endl;
}
8:動的整数2次元配列
#include <iostream>

int main() {
    int rows = 3, cols = 4;
    int** matrix = new int*[rows];
    for (int i = 0; i < rows; ++i) {
        matrix[i] = new int[cols];
        for (int j = 0; j < cols; ++j) {
            matrix[i][j] = i * cols + j;
        }
    }

    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            std::cout << matrix[i][j] << " ";
        }
        std::cout << std::endl;
    }

    for (int i = 0; i < rows; ++i) delete[] matrix[i];
    delete[] matrix;
}
9:複合
#include <iostream>
#include <vector>

#define SIZE 5

typedef std::vector<int> IntVector;

int main() {
    int* arr = new int[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        arr[i] = i * 10;
    }

    IntVector* vec = new IntVector();
    for (int i = 0; i < SIZE; ++i) {
        vec->push_back(arr[i]);
    }

    for (int i = 0; i < vec->size(); ++i) {
        std::cout << (*vec)[i] << std::endl;
    }

    delete[] arr;
    delete vec;

    return 0;
}

解答編はこちら↓

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?