C ++, 복사가 벡터로 설정
나는 복사해야합니다 std::set
에 std::vector
:
std::set <double> input;
input.insert(5);
input.insert(6);
std::vector <double> output;
std::copy(input.begin(), input.end(), output.begin()); //Error: Vector iterator not dereferencable
문제는 어디에 있습니까?
당신은 사용해야합니다 back_inserter
:
std::copy(input.begin(), input.end(), std::back_inserter(output));
std::copy
삽입하는 컨테이너에 요소를 추가하지 않습니다. 컨테이너에는 반복자가 있습니다. 이 때문에 출력 반복자를 직접로 전달하는 경우 std::copy
입력 범위를 보유하기에 충분한 범위를 가리키는 지 확인해야합니다.
std::back_inserter
push_back
각 요소의 컨테이너를 호출하는 출력 반복자를 작성하여 각 요소가 컨테이너에 삽입됩니다. 또는 std::vector
복사 할 범위를 보유 하기에 충분한 수의 요소를 만들 수도 있습니다 .
std::vector<double> output(input.size());
std::copy(input.begin(), input.end(), output.begin());
또는 std::vector
범위 생성자를 사용할 수 있습니다 .
std::vector<double> output(input.begin(), input.end());
반복자를 취하는 벡터의 생성자를 사용하십시오.
std::set<T> s;
//...
std::vector v( s.begin(), s.end() );
v에서 s의 내용을 원하고 v에 데이터를 복사하기 전에 v에 아무것도 없다고 가정합니다.
다음을 사용하는 또 다른 대안이 있습니다 vector::assign
.
theVector.assign(theSet.begin(), theSet.end());
벡터 객체에 세트의 내용을 담을 공간이 충분하지 않습니다.
std::vector<double> output(input.size());
std::copy(input.begin(), input.end(), output.begin());
std::copy
빈 용기에 넣을 수 없습니다. 그렇게하려면 다음과 같이 insert_iterator를 사용해야합니다.
std::set<double> input;
input.insert(5);
input.insert(6);
std::vector<double> output;
std::copy(input.begin(), input.end(), inserter(output, output.begin()));
가장 효율적인 방법은 요소를 미리 할당 한 다음 배치하는 것입니다.
template <typename T>
std::vector<T> VectorFromSet(const std::set<T>& from)
{
std::vector<T> to;
to.reserve(from.size());
for (auto const& value : from)
to.emplace_back(value);
return to;
}
이렇게하면 기본 생성자를 먼저 호출하는 대신 위에 나열된 다른 솔루션에 대한 할당 할당 연산자를 사용하는 대신 모든 요소에 대해 복사 생성자를 호출합니다. 아래에 더 많은 설명이 있습니다.
back_inserter may be used but it will invoke push_back() on the vector (https://en.cppreference.com/w/cpp/iterator/back_insert_iterator). emplace_back() is more efficient because it avoids creating a temporary when using push_back(). It is not a problem with trivially constructed types but will be a performance implication for non-trivially constructed types (e.g. std::string).
We need to avoid constructing a vector with the size argument which causes all elements default constructed (for nothing). Like with solution using std::copy(), for instance.
And, finally, vector::assign() method or the constructor taking the iterator range are not good options because they will invoke std::distance() (to know number of elements) on set iterators. This will cause unwanted additional iteration through the all set elements because the set is Binary Search Tree data structure and it does not implement random access iterators.
Hope that helps.
참고URL : https://stackoverflow.com/questions/5034211/c-copy-set-to-vector
'IT story' 카테고리의 다른 글
배열이 다차원인지 아닌지 확인? (0) | 2020.07.03 |
---|---|
Twitter 부트 스트랩에서 스타일을 덮어 쓰는 방법 (0) | 2020.07.03 |
div 내에서 (배경) 이미지를 가운데에 배치하는 방법은 무엇입니까? (0) | 2020.07.03 |
CSS 클래스 .foo.bar (공백 없음)와 .foo .bar (공백 있음)의 차이점 (0) | 2020.07.03 |
SQL을 사용하여 Postgres db 8.1의 모든 시퀀스 나열 (0) | 2020.07.03 |