C++メモ
mutable
volatile
コンパイラによる最適化を抑止する。
1 2 3 4 5 6 |
int some_int = 100; while(some_int == 100) // コンパイラにより最適化されて常に true だったり、while{} がデッドストリップされる。 { //your code } |
volatile int some_int = 100; と宣言するとコンパイラによる最適化が抑止される。
some_int が別スレッドなどコンパイラの知り得ない所で変更される場合は volatile を付けて宣言するとよい。
std::lower_bound
サブベクトルの組み合わせを作る
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include <iostream> #include <vector> // 再帰的に組み合わせを生成する関数 void generateCombinations( const std::vector<std::vector<int>>& container, std::vector<int>& combination, int depth, std::vector<std::vector<int>>& results) { if (depth == container.size()) { // 全ての深さを処理したら結果に追加 results.push_back(combination); return; } // 現在の深さの各要素に対して再帰呼び出し for (int val : container[depth]) { combination[depth] = val; generateCombinations(container, combination, depth + 1, results); } } int main() { // 入力のサンプル std::vector<std::vector<int>> container = { {1, 2}, {3, 4}, {5, 6} }; std::vector<int> combination(container.size()); std::vector<std::vector<int>> results; // 組み合わせを生成 generateCombinations(container, combination, 0, results); // 結果を出力 for (const auto& comb : results) { for (int val : comb) { std::cout << val << " "; } std::cout << std::endl; } return 0; } |
コメントを残す