0
0

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++の基本的なコードまとめ

0
Last updated at Posted at 2026-04-26

iPadでまとめてたのですが,毎回振り返るのが大変なので,この記事でまとめてわかんなくなったときは参照したいと思います.

stdの関数を使うための魔法

#include<bits/stdc++.h>
using namespace std;

C++における演算ルール

7 / 2 = 3 // 切り捨て
7.0 / 2.0 = 3.5 //float

論理演算子

if not => if(!(条件))
and => &&
or => ||

bit演算

左に向いているので左シフトと覚える.2進数の1が右から左に行くイメージ

2^n => (1 << n)
//bit探索
for(int i = 0; i <(1 << n); i++){
  // iのjビットが0であるための条件
  if((i & (1 << j)) == 0)

breakとcontinue

  • while, for文に入れることができる
  • break:そのループを行っているスコープ全体から抜けることができる.
  • contine:そのスコープのcontinue以下の処理をカットし,もう一度ループを行う.
  • ループを抜ける処理としてreturnがあるが,int main()を抜け出してしまう.つまりその関数の処理が終了する

文字列操作

//定義
string s = "abc";
//n文字目
s[2] = 'b'; //char型
//文字数
s.size(); //pythonなら.length()
//空かどうか
s.empty(); => false
//挿入
s.insert(2,"cd"); => s = "abccd"
//繰り返し
string(x,'-"); //x回'-'を繰り返すコンストラクタ

配列

一次元配列

//定義
vector<int> A(要素数);
//空のときは要素数はいらない
//使える操作
A[i];
A.size();
A.push_back(a);
A.pop_back(a);
sort(A.begin(), A.end())// A[0]が一番小さくなる

二次元配列

//定義
vector<vector<int>> B(W, vector<int>(H,0));
//Hこの要素数の縦ベクトルがW列ある配列,どの値も0
//i行j列目へのアクセス
B[i][j];

型変換

//文字列→整数
int("123");
//整数→文字列;
to_string(num);
0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?