誰かがこのコードの仕組みを理解できたら助けてくれますか?私が言うことができるものから(ペアは、二つの整数aを有する構造であり、b)ペア初期化ベクトル
Pair a[] = {{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}}; int n = sizeof(a)/sizeof(a[0]); vector<Pair> arr(a, a + n);
、それは別の配列内の各ペアを置くが、私は前に宣言のこの種のを見たことがありません。
誰かがこのコードの仕組みを理解できたら助けてくれますか?私が言うことができるものから(ペアは、二つの整数aを有する構造であり、b)ペア初期化ベクトル
Pair a[] = {{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}}; int n = sizeof(a)/sizeof(a[0]); vector<Pair> arr(a, a + n);
、それは別の配列内の各ペアを置くが、私は前に宣言のこの種のを見たことがありません。
クラステンプレートstd::vector
このコンストラクタが使用さ
vector<Pair> arr(a, a + n);
この宣言においてこのようコンストラクタ
template <class InputIterator>
vector(InputIterator first, InputIterator last, const Allocator& = Allocator());
を有しています。 a
およびa + n
は、[a, a + n)
の範囲を指定します。この範囲の要素は、ベクトルを初期化するために使用されます。この宣言
Pair a[] = {{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}};
として
は、それは各要素がブレースのリストを使用して初期化された配列の宣言です。ユーザー定義型Pair
は、集約型であるか、2つの引数を受け入れるコンストラクターを持っているようです。ここで
は実証プログラムは、その出力が
{ 5, 29 } { 39, 40 } { 15, 28 } { 27, 40 } { 50, 90 }
の代わりにこれらのステートメント
size_t n = sizeof(a)/sizeof(a[0]);
std::vector<Pair> arr(a, a + n);
であるあなただけ
std::vector<Pair> arr(std::begin(a), std::end(a));
を書くことができ
#include <iostream>
#include <vector>
struct Pair
{
int x;
int y;
};
int main()
{
Pair a[] =
{
{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}
};
size_t n = sizeof(a)/sizeof(a[0]);
std::vector<Pair> arr(a, a + n);
for (const Pair &p : arr)
{
std::cout << "{ " << p.x
<< ", " << p.y
<< " }" << ' ';
}
std::cout << std::endl;
return 0;
}
ですここ
別の例証的なプログラムは、上記のように、その出力が同じである
#include <iostream>
#include <vector>
#include <iterator>
struct Pair
{
int x;
int y;
};
int main()
{
Pair a[] =
{
{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}
};
std::vector<Pair> arr(std::begin(a), std::end(a));
for (const Pair &p : arr)
{
std::cout << "{ " << p.x
<< ", " << p.y
<< " }" << ' ';
}
std::cout << std::endl;
return 0;
}
あります。
「ペア」とは何ですか?正確に何を理解していないのですか?あなたは 'ベクトル'に精通していますか? 'vector'のドキュメントをチェックしましたか? –