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>
#include <vector>

int main() {
    std::vector<int> arr(5);
    for (int i = 0; i < arr.size(); ++i) {
        arr[i] = i * i;
    }

    for (auto val : arr) {
        std::cout << val << std::endl;
    }
}
  • new/delete を削除
  • std::vector に置き換え
  • range-based for で出力

2:文字列の配列
#include <iostream>
#include <vector>
#include <string>

int main() {
    std::vector<std::string> names = {"Alice", "Bob", "Carol"};

    for (const auto& name : names) {
        std::cout << name << std::endl;
    }
}
  • Cスタイル文字列を std::string に置き換える
  • std::vector<std::string> を使う
  • range-based for を使う

3:構造体配列の初期化と出力
#include <iostream>
#include <vector>

struct Point {
    int x;
    int y;
};

int main() {
    std::vector<Point> points = {
        {1, 2}, {3, 4}, {5, 6}
    };

    for (const auto& pt : points) {
        std::cout << pt.x << ", " << pt.y << std::endl;
    }
}
  • new/delete を削除
  • std::vector<Point> で管理
  • range-based for & const参照で出力

4:ポインタの多重管理
#include <iostream>
#include <memory>

int main() {
    auto a = std::make_unique<int>(10);
    auto b = std::make_unique<int>(20);

    int sum = *a + *b;
    std::cout << sum << std::endl;
}
  • std::unique_ptr で自動管理
  • delete不要
  • 生ポインタ操作をなくして安全

5:整数配列の逆順出力
#include <iostream>
#include <vector>

int main() {
    std::vector<int> arr = {10, 20, 30, 40, 50};

    // 逆順ループ(添字版)
    for (int i = arr.size() - 1; i >= 0; --i) {
        std::cout << arr[i] << std::endl;
    }

    // 逆イテレータ版(モダンC++)
    for (auto it = arr.rbegin(); it != arr.rend(); ++it) {
        std::cout << *it << std::endl;
    }
}
  • std::vector に置き換え
  • 逆順ループは rbegin() / rend() を使うと range-based for に近い

6:文字列の動的配列
#include <iostream>
#include <vector>
#include <string>

int main() {
    std::vector<std::string> names = {"Tom", "Bob", "Alice"};

    for (const auto& name : names) {
        std::cout << name << std::endl;
    }
}
  • char* + new/delete を削除
  • std::vector<std::string> + range-based for に置き換え

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; // 10, 2
    std::cout << p2.x << ", " << p2.y << std::endl; // 10, 2
}
  • コピーではなく参照を使う
  • const auto& を使えば読み取り専用の参照にもできる

8:動的整数2次元配列
#include <iostream>
#include <vector>

int main() {
    int rows = 3, cols = 4;
    std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols));

    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            matrix[i][j] = i * cols + j;
        }
    }

    // 出力(range-based for + const参照)
    for (const auto& row : matrix) {
        for (const auto& val : row) {
            std::cout << val << " ";
        }
        std::cout << std::endl;
    }
}
  • new/delete を削除
  • std::vector<std::vector<int>> で2次元配列管理
  • range-based for + const参照で出力

9:複合
#include <iostream>
#include <vector>

constexpr int SIZE = 5;

using IntVector = std::vector<int>;

int main() {
    // 配列作成
    std::vector<int> arr(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        arr[i] = i * 10;
    }

    // vec にコピー
    IntVector vec;
    for (auto val : arr) {
        vec.push_back(val);
    }

    // 出力
    for (auto val : vec) {
        std::cout << val << std::endl;
    }

    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?