C++
#6
TIOBE#2
PYPL#4
GitHub#9
RedMonk#7
IEEESpectrum#4
JetBrains#8
プログラミング言語
C++(シープラスプラス)
概要
C++は、C言語を拡張して作られたオブジェクト指向プログラミング言語です。C言語の高性能性を維持しながら、オブジェクト指向、テンプレート、例外処理などの機能を追加し、システムプログラミングからアプリケーション開発まで幅広い分野で使用されています。
詳細
C++は1985年にBjarne Stroustrupによって開発され、「C with Classes」から発展しました。メモリ管理の詳細な制御が可能で、高いパフォーマンスが要求される分野で特に重要な役割を果たしています。
モダンC++(C++11以降)では、auto型推論、ラムダ式、スマートポインタ、移動セマンティクス、範囲ベースforループなどの機能が追加され、より安全で表現力豊かなコードが書けるようになりました。C++14、C++17、C++20と継続的に新機能が追加され、現代的な開発にも対応しています。
書き方の例
Hello World
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
クラスとオブジェクト
#include <iostream>
#include <string>
class Person {
private:
std::string name;
int age;
public:
// コンストラクタ
Person(const std::string& n, int a) : name(n), age(a) {}
// ゲッター
const std::string& getName() const { return name; }
int getAge() const { return age; }
// メソッド
void introduce() const {
std::cout << "こんにちは、" << name << "です。"
<< age << "歳です。" << std::endl;
}
};
int main() {
Person person("太郎", 25);
person.introduce();
return 0;
}
テンプレート
#include <iostream>
#include <vector>
// 関数テンプレート
template<typename T>
T findMax(const std::vector<T>& vec) {
if (vec.empty()) {
throw std::invalid_argument("Empty vector");
}
T max = vec[0];
for (const T& element : vec) {
if (element > max) {
max = element;
}
}
return max;
}
// クラステンプレート
template<typename T>
class Stack {
private:
std::vector<T> elements;
public:
void push(const T& element) {
elements.push_back(element);
}
T pop() {
if (elements.empty()) {
throw std::runtime_error("Stack is empty");
}
T top = elements.back();
elements.pop_back();
return top;
}
bool empty() const {
return elements.empty();
}
};
int main() {
std::vector<int> numbers = {1, 5, 3, 9, 2};
std::cout << "最大値: " << findMax(numbers) << std::endl;
Stack<int> stack;
stack.push(10);
stack.push(20);
std::cout << "ポップ: " << stack.pop() << std::endl;
return 0;
}
モダンC++の機能
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
class Resource {
private:
std::string data;
public:
Resource(const std::string& d) : data(d) {
std::cout << "Resource created: " << data << std::endl;
}
~Resource() {
std::cout << "Resource destroyed: " << data << std::endl;
}
const std::string& getData() const { return data; }
};
int main() {
// auto型推論
auto number = 42;
auto text = std::string("Hello");
// スマートポインタ
auto resource = std::make_unique<Resource>("データ");
std::cout << "Resource data: " << resource->getData() << std::endl;
// 範囲ベースforループ
std::vector<int> numbers = {1, 2, 3, 4, 5};
for (const auto& num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
// ラムダ式
auto isEven = [](int n) { return n % 2 == 0; };
auto evenCount = std::count_if(numbers.begin(), numbers.end(), isEven);
std::cout << "偶数の個数: " << evenCount << std::endl;
return 0;
}
STLコンテナとアルゴリズム
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <numeric>
int main() {
// vector
std::vector<int> numbers = {5, 2, 8, 1, 9};
// ソート
std::sort(numbers.begin(), numbers.end());
// 各要素を2倍にする
std::transform(numbers.begin(), numbers.end(), numbers.begin(),
[](int n) { return n * 2; });
// 合計を計算
int sum = std::accumulate(numbers.begin(), numbers.end(), 0);
std::cout << "合計: " << sum << std::endl;
// map
std::map<std::string, int> scores;
scores["Alice"] = 85;
scores["Bob"] = 92;
scores["Charlie"] = 78;
for (const auto& [name, score] : scores) {
std::cout << name << ": " << score << std::endl;
}
return 0;
}
メモリ管理
#include <iostream>
#include <memory>
class Data {
public:
int value;
Data(int v) : value(v) {
std::cout << "Data constructed: " << value << std::endl;
}
~Data() {
std::cout << "Data destructed: " << value << std::endl;
}
};
int main() {
// RAWポインタ(非推奨)
Data* rawPtr = new Data(10);
delete rawPtr; // 手動削除が必要
// スマートポインタ(推奨)
{
auto uniquePtr = std::make_unique<Data>(20);
auto sharedPtr = std::make_shared<Data>(30);
auto anotherShared = sharedPtr; // 参照カウント: 2
std::cout << "参照カウント: " << sharedPtr.use_count() << std::endl;
} // スコープを抜けると自動的に削除
return 0;
}
メリット・デメリット
メリット
- 高性能: 低レベルなメモリ管理とCPU効率の最適化が可能
- 豊富な機能: オブジェクト指向、テンプレート、STLなど強力な機能
- 移植性: 多くのプラットフォームで利用可能
- 大規模な生態系: 豊富なライブラリとフレームワーク
- 業界標準: システムプログラミングやゲーム開発での標準言語
- モダンな機能: C++11以降の新機能により開発効率が向上
デメリット
- 学習コストが高い: メモリ管理や複雑な言語仕様の理解が必要
- コンパイル時間: 大規模プロジェクトでのコンパイル時間が長い
- メモリ安全性: 手動メモリ管理によるバグのリスク
- 複雑性: 多機能ゆえの言語仕様の複雑さ
- 開発速度: 他の高水準言語と比較して開発時間が長い
主要リンク
- ISO C++公式サイト
- cppreference.com - C++リファレンス
- Microsoft C++ドキュメント
- GCC - GNU Compiler Collection
- Clang C++コンパイラ
ランキング情報
- 総合ランキング: 6位
- TIOBE Index: 2位
- PYPL PopularitY: 4位
- GitHub使用率: 9位
- RedMonk言語ランキング: 7位
- IEEE Spectrum: 4位
- JetBrains開発者調査: 8位
C++は、高性能が要求されるシステム開発、ゲーム開発、組み込みシステムなどで重要な役割を果たす、強力なプログラミング言語です。