코드 인 스 턴 스 를 통 해 c+vector 상용 방법 을 분석 합 니 다.

6224 단어 c + +vector
1.c++vector 요소 마다 특정 값 추가(c++vector add a constant value for each element)
https://stackoverflow.com/questions/4461446/stl-way-to-add-a-constant-value-to-a-stdvector

vector<int> x = {0, 30, 80, 100, 120, 150, 190, 220, 250};
//transform              ,bind2nd                      
transform(x.begin(), x.end(), x.begin(), bind2nd(plus<int>(), 1));
//  x  
copy(x.begin(), x.end(), ostream_iterator<int>(cout, " "));
결과:x={1 31 81 101 121 151 191 221 251}
2.c++는 vector 에 어떤 요소 가 존재 하 는 지 판단 합 니 다(c+는 vector 에 요소 가 존재 하 는 지 판단 합 니 다)
https://www.techiedelight.com/check-vector-contains-given-element-cpp/

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
  std::vector<int> v = { 4, 7, 5, 2, 6, 9 };
  int key = 6;

  if (std::count(v.begin(), v.end(), key))
    std::cout << "Element found";
  else
    std::cout << "Element not found";

  return 0;
}
결과:Element found
3.c++vector지정 한 개수 의 순서 목록 생 성(c++generate a sequential vectorof special numbers)
https://stackoverflow.com/questions/17694579/use-stdfill-to-populate-vector-with-increasing-numbers
 std::vector seq(10);
 // numeric 헤더 파일 에 정 의 된 iota()함수 템 플 릿 은 연속 T 형식 값 으로 시퀀스 를 채 웁 니 다.
 std::iota(seq.begin(), seq.end(), 0);
결과:seq={0,1,2,3,4,5,6,7,8,9}
4.c++한 문장 으로 vector 정 보 를 인쇄 합 니 다(c++print out vector by one statement).
https://stackoverflow.com/questions/10750057/how-to-print-out-the-contents-of-a-vector
 vector x = {1, 2, 3, 4};
 //istream_iterator 는 입력 흐름 에서 연속 요 소 를 읽 는 데 사 용 됩 니 다.
 copy(x.begin(), x.end(), ostream_iterator(cout, " "));
결과:1,2,3,4
5.c++는 vector에서 요소 의 최대 값 과 최소 값,최대 값 과 최소 값 의 색인 위 치 를 얻 습 니 다(c++get the maximum and minimum values of the elements in vectorand the index positions)
https://riptutorial.com/cplusplus/example/11151/find-max-and-min-element-and-respective-index-in-a-vector

vector<int> row_y = { 502, 263, 684, 324, 979 };

//          
int row_y_max_index = max_element(row_y.begin(), row_y.end()) - row_y.begin();
cout << "row_y_max_index = " << row_y_max_index << endl;
int row_y_max_value = *max_element(row_y.begin(), row_y.end());
cout << "row_y_max_value = " << row_y_max_value << endl;

//          
int row_y_min_index = min_element(row_y.begin(), row_y.end()) - row_y.begin();
cout << "row_y_min_index = " << row_y_min_index << endl;
int row_y_min_value = *min_element(row_y.begin(), row_y.end());
cout << "row_y_min_value = " << row_y_min_value << endl;
결과 반환:
row_y_max_index = 4
row_y_max_value = 979
row_y_min_index = 1
row_y_min_value = 263
6.c++vector 에 두 개의 vector 추가(c++append a vector to vector)
https://stackoverflow.com/questions/2551775/appending-a-vector-to-a-vector
 vector x = {0, 30, 80, 100, 120, 150, 190, 220, 250};
 vector y = {100};
 y.insert(y.end(), x.begin(), x.end());
결과:y={100,0,30,80,100,120,150,190,220,250}
7.c++복사 벡터(c++복사 벡터)
https://www.geeksforgeeks.org/ways-copy-vector-c/
vector x = {0, 30, 80, 100, 120, 150, 190, 220, 250};
 vector y;
 y.assign(x.begin(), x.end());
결과:y={0,30,80,100,120,150,190,220,250}
8.c++vector 주어진 색인 에 따라 요소 삭제(c++vector delete element based on a given index)
https://iq.opengenus.org/ways-to-remove-elements-from-vector-cpp/
두 번 째 색인 값 과 다섯 번 째 색인 값 을 삭제 하려 면 다음 문 구 를 사용 할 수 있 습 니 다.
 vector x = {0, 30, 80, 150, 120, 150, 30, 220, 80};
 //remove(x.begin(), x.end(), 80);
 x.erase(x.begin() + 2, x.begin() + 5 + 1);
결과:x={0,30,30,220,80}
9.c++벡터 에 지 정 된 모든 요 소 를 삭제 합 니 다(c++벡터 에 지 정 된 모든 요 소 를 삭제 합 니 다)
https://www.techiedelight.com/erase-elements-vector-cpp/
 vector x = {0, 30, 150, 30, 220, 80};
 //vector 의 remove 역할 은 value 와 같은 요 소 를 vector 의 끝 에 두 는 것 이지 만 vector 의 size 를 줄 이지 않 습 니 다.
 //vector 에서 erase 의 역할 은 특정한 위치 position 나 한 구간(begin,end)의 요 소 를 삭제 하고 size 를 줄 이 는 것 입 니 다.
 x.erase(remove(x.begin(), x.end(), 30), x.end());
결과:x={0 150 220 80}
10.c++는 vector 의 특정한 요소 가 나타 나 는 횟수 를 통계 합 니 다(C++count the number of occurrences of an element in vector)
https://www.geeksforgeeks.org/std-count-cpp-stl/
 vector x = { 0, 3, 5, 6, 3, 2, 3 };
 int n = count(x.begin(), x.end(), 3);
결과 n=3

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기