しかしまあ、何ですなあ~は小枝。
C++やり始めてvectorをよく使う。Cの経験の長いワテにはvectorが便利なのだが、使い方を良く忘れるのでメモ。
1. 二つのvector<T>を結合する関数。テンプレート関数で作ってみた。
template<typename T> vector<T> vector_concat(const vector<T>&A, const vector<T>&B)
{ vector<T> AB; AB.reserve(A.size() + B.size()); // preallocate memory AB.insert(AB.end(), A.begin(), A.end()); AB.insert(AB.end(), B.begin(), B.end()); return AB; }
2. vector<wstring>の中で空文字列な要素を削除する関数
わざわざ、関数にする必要は無いかもしれないが、よく使うので関数化した。
vector<wstring> vector_remove_empty(vector<wstring> &v) { v.erase(remove(v.begin(), v.end(), L""), v.end()); return v; }
void main()
{
vector<string> v = { "abc", "", "de", "", "fgh" };
for (const wstring& s : v)
wcout << s << endl;
vector<wstring>v2 = vector_remove_empty(v);
for (const wstring& s : v2)
wcout << s << endl; // "abc", "de", "fgh" }
}