2
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++書き換えパターン集

Last updated at Posted at 2025-09-01

1. ポインタ管理

// Before
Foo* f = new Foo();
f->DoSomething();
delete f;

// After
auto f = std::make_unique<Foo>();
f->DoSomething();  // delete不要

2. NULL → nullptr

// Before
Bar* b = NULL;

// After
Bar* b = nullptr;

3. #define 定数 → constexpr

// Before
#define MAX_SIZE 256

// After
constexpr int MaxSize = 256;

4. typedef → using

// Before
typedef std::map<int, std::string> MyMap;

// After
using MyMap = std::map<int, std::string>;

5. 古いfor文 → 範囲for

// Before
for (int i = 0; i < vec.size(); ++i) {
    vec[i].print();
}

// After
for (auto& item : vec) {
    item.print();
}

6. 明示的な型 → auto

// Before
std::map<int, std::string>::iterator it = myMap.find(3);

// After
auto it = myMap.find(3);

7. RAII(リソース管理)

// Before
FILE* fp = fopen("data.txt", "r");
if (fp) {
    // 読み込み処理
    fclose(fp); // 閉じ忘れるとリーク
}

// After
std::ifstream file("data.txt");
if (file.is_open()) {
    // 読み込み処理
} // 自動でクローズ

古いc++の例

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>

#define SIZE 5
#define MSG "Hello World"

typedef struct Point {
    int x;
    int y;
} Point;

class Sample {
public:
    Sample(const char* msg) {
        strcpy(buf, msg);
    }
    void print() {
        std::cout << buf << std::endl;
    }
private:
    char buf[64];
};

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

    CArray<Point*> points;
    for (int i = 0; i < SIZE; ++i) {
        Point* p = new Point();
        p->x = i;
        p->y = i * 2;
        points.Add(p);
    }

    const char* str = NULL;
    if (str == NULL) {
        str = MSG;
    }

    typedef std::vector<int> IntVector;
    IntVector vec;
    for (int i = 0; i < SIZE; ++i) {
        vec.push_back(arr[i]);
    }

    char chars[] = "ABCDE";
    for (int i = 0; i < strlen(chars); ++i) {
        std::cout << chars[i] << std::endl;
    }

    FILE* fp = fopen("test.txt", "r");
    if (fp) {
        char line[256];
        while (fgets(line, sizeof(line), fp)) {
            std::cout << line;
        }
        fclose(fp);
    }

    Sample* s = new Sample(MSG);
    s->print();
    delete s;

    for (int i = 0; i < SIZE; ++i) {
        delete points[i];
    }
    delete[] arr;

    return 0;
}

モダンC++への書き換え例

#include <iostream>
#include <vector>
#include <string>
#include <array>
#include <fstream>
#include <memory>

constexpr int SIZE = 5;
constexpr auto MSG = "Hello World";

struct Point {
    int x;
    int y;
};

class Sample {
public:
    explicit Sample(const std::string& msg) : buf(msg) {}
    void print() const {
        std::cout << buf << std::endl;
    }
private:
    std::string buf;
};

int main() {
    auto arr = std::make_unique<std::array<int, SIZE>>();
    for (int i = 0; i < SIZE; ++i) {
        (*arr)[i] = i * 10;
    }

    std::vector<std::unique_ptr<Point>> points;
    for (int i = 0; i < SIZE; ++i) {
        auto p = std::make_unique<Point>();
        p->x = i;
        p->y = i * 2;
        points.push_back(std::move(p));
    }

    const char* str = nullptr;
    if (str == nullptr) {
        str = MSG;
    }

    using IntVector = std::vector<int>;
    IntVector vec;
    for (auto val : *arr) {
        vec.push_back(val);
    }

    std::string chars = "ABCDE";
    for (const auto& c : chars) {
        std::cout << c << std::endl;
    }

    std::ifstream ifs("test.txt");
    if (ifs) {
        std::string line;
        while (std::getline(ifs, line)) {
            std::cout << line << std::endl;
        }
    }

    auto s = std::make_unique<Sample>(MSG);
    s->print();

    return 0;
}

実際に書き換えてみよう↓

2
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
2
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?