0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

c++のvector使うときのメモ

Last updated at Posted at 2023-07-23

※「この記事は~」的な言い訳はめんどいので省略です。
正確な情報が必要な方は、ちゃんとした記事を参照ください。

vector使うときのメモ。

for文

vector<int> v = {1,2,3};
for (auto &itr : v) {
    printf("%d\n", itr);
}

pairの場合

vector<pair<char, int>> v = {{'a',1}, {'b',2}, {'c',3}};
for (auto &[c, n] : v) {
    printf("%c %d\n", c, n);
}

sort

昇順sort

vector<int> v = {3,1,2};
sort(v.begin(), v.end());
for (auto &itr : v) {
    printf("%d\n", itr); // 1, 2, 3
}

降順sort

vector<int> v = {3,1,2};
sort(v.begin(), v.end(), greater<int>{});
for (auto &itr : v) {
    printf("%d\n", itr); // 3, 2, 1
}

文字列もsortできる

vector<string> str = {"zac", "abc", "aaa", "zxx"};
sort(str.begin(), str.end());
for(auto i: str) {
    printf("%s\n", i.c_str()); // aaa, abc, zac, axx
}

pairのsortはfirstを優先してsortする

vector<pair<char, int>> v = {{'a',2}, {'c',1}, {'a',1}};
sort(v.begin(), v.end());
for (auto &[c, n] : v) {
    printf("%c %d\n", c, n); // a 1, a 2, c 1
}

ラムダ式でsortの条件を指定できる

vector<int> v = {3,1,2};
// trueを返すと x が左側になる
sort(v.begin(), v.end(), [](auto x, auto y){return (x > y);});
for (auto &itr : v) {
    printf("%d\n", itr); // 3, 2, 1
}
vector<pair<char, int>> v = {{'a',2}, {'c',1}, {'a',1}};

auto func = [](auto x, auto y) {
    if (x.second > y.second) {
        return true;
    }
    else if (x.second < y.second) {
        return false;
    }
    else if (x.first > y.first) {
        return true;
    }
    else {
        return false;
    }
}; // この例ではsecondで降順sortしたあと、firstで降順sortする

sort(v.begin(), v.end(), func);
for (auto &[c, n] : v) {
    printf("%c %d\n", c, n); // a 2, c 1, a 1
}

重複削除

vector<int> v = {5,2,9,6,1};
sort(v.begin(), v.end()); // sortは必要。

v.erase(std::unique(v.begin(), v.end()), v.end()); // uniqすると後ろにゴミが残るので、eraseも必要

連結

a.insert(a.end(), b.begin(), b.end()); // aのうしろにbを連結。

二分探索

binary_search と lower_bound でだいたい足りる気がするけど

vector<int> v = {5,2,9,6,1};
sort(v.begin(), v.end()); // sortは必要。

// 2文探索して、見つかったらtrueを返す
bool result = binary_search(v.begin(), v.end(), 3);
printf("%s\n", result ? "ok" : "ng"); // ng

// 5以上の要素のうち、いちばん最初のイテレータを返す。
auto itr = lower_bound(v.begin(), v.end(), 5);
printf("%d\n", *itr); // 5

// イテレータから、配列で使うindexを求めたいなら、begin()とのdistanceをとる。
int i = distance(v.begin(), itr);
printf("%d\n", i); // 2

たまにupper_boundを使いたいときもある

vector<int> v = {5,2,9,6,1};
sort(v.begin(), v.end()); // sortは必要。

// 5より大きい要素のうち、いちばん最初のイテレータをかえす。
auto itr = upper_bound(v.begin(), v.end(), 5);
printf("%d\n", *itr); // 6
0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?