표준::벡터::삽입() 지정된 위치의 요소 앞에 새 요소를 삽입하여 삽입된 요소 수만큼 컨테이너 크기를 효과적으로 늘리는 C++ STL의 내장 함수입니다.
시간 복잡도 – 선형, O(N)
삽입 함수는 다음과 같은 여러 경우에 작동하도록 오버로드됩니다.
- 주어진 인덱스에 요소를 삽입합니다.
- 요소를 여러 번 삽입합니다.
- 주어진 인덱스에 요소 범위를 삽입합니다.
1. 주어진 인덱스에 요소 삽입
Vector의 insert() 구문
vector_name.insert (position, val);>
매개변수
이 함수는 아래에 지정된 두 개의 매개변수를 허용합니다.
- 위치 – 삽입이 완료될 위치를 가리키는 반복자를 지정합니다.
- 발 – 삽입할 값을 지정합니다.
벡터의 insert() 예
C++
// C++ program to illustrate the function of> // vector_name.insert(position,val)> #include> using> namespace> std;> > int> main()> {> > > // Initialising the vector> > vector<> int> >벡터이름{ 1, 2, 3, 4, 5 };> > > // Printing out the original vector> > cout <<> 'Original vector :
'> ;> > for> (> auto> x : vector_name)> > cout << x <<> ' '> ;> > cout <<> '
'> ;> > > // Inserting the value 100 at position 3(0-based> > // indexing) in the vector> > vector_name.insert(vector_name.begin() + 3, 100);> > > // Printing the modified vector> > cout <<> 'Vector after inserting 100 at position 3 :
'> ;> > for> (> auto> x : vector_name)> > cout << x <<> ' '> ;> > cout <<> '
'> ;> > > // Inserting the value 500 at position 1(0-based> > // indexing) in the vector> > vector_name.insert(vector_name.begin() + 1, 500);> > > // Printing the modified vector> > cout <<> 'Vector after inserting 500 at position 1 :
'> ;> > for> (> auto> x : vector_name)> > cout << x <<> ' '> ;> > return> 0;> }> > // This code is contributed by Abhijeet Kumar(abhijeet19403)> |
과일이 몇 개 있어요?
>
>산출
Original vector : 1 2 3 4 5 Vector after inserting 100 at position 3 : 1 2 3 100 4 5 Vector after inserting 500 at position 1 : 1 500 2 3 100 4 5>
2. 주어진 인덱스에 여러 요소 삽입
Vector의 insert() 구문
vector_name.insert(position, size, val)>
매개변수
이 함수는 아래와 같이 지정된 세 가지 매개변수를 허용합니다.
- 위치 – 삽입이 완료될 위치를 가리키는 반복자를 지정합니다.
- 크기 – 지정된 위치에 val이 삽입되는 횟수를 지정합니다.
- 발 – 삽입할 값을 지정합니다.
Vector의 insert() 예
C++
// C++ program to illustrate the function of> // vector_name.insert(position,size,val)> #include> using> namespace> std;> > int> main()> {> > > // Initialising the vector> > vector<> int> >벡터이름{ 1, 2, 3, 4, 5 };> > > // Printing out the original vector> > cout <<> 'Original vector :
'> ;> > for> (> auto> x : vector_name)> > cout << x <<> ' '> ;> > cout << endl;> > > // Inserting the value 100, 4 times starting at position> > // 3> > vector_name.insert(vector_name.begin() + 3, 4, 100);> > > // Printing the modified vector> > cout <<> 'Vector after inserting 100, 4 times, starting '> > 'at position 3 :
'> ;> > for> (> auto> x : vector_name)> > cout << x <<> ' '> ;> > return> 0;> }> > // This code contributed by Harsh Singh (hsnooob)> |
>
스리데비
>산출
Original vector : 1 2 3 4 5 Vector after inserting 100, 4 times, starting at position 3 : 1 2 3 100 100 100 100 4 5>
3. 주어진 인덱스에 요소 범위 삽입
벡터 삽입()의 구문
vector_name.insert(position, iterator1, iterator2)>
매개변수
이 함수는 아래에 지정된 세 가지 매개변수를 허용합니다.
- 위치 – 벡터에서 삽입이 수행될 위치를 지정합니다.
- 반복자1 – 요소가 삽입될 시작 위치를 지정합니다.
- 반복자2 – 요소가 삽입될 끝 위치를 지정합니다.
벡터 삽입()의 예
C++
// C++ program to illustrate the function of> // vector_name.insert(position,itr1,itr2)> #include> using> namespace> std;> > int> main()> {> > > // Initialising the vector> > vector<> int> >원본{ 1, 2, 3, 4, 5 };> > > vector<> int> >온도{2, 5, 9, 0, 3, 10 };> > > // Printing out the original vector> > cout <<> 'Original vector :
'> ;> > for> (> auto> x : original)> > cout << x <<> ' '> ;> > cout << endl;> > > // Inserting the portion of temp vector in original> > // vector temp.begin()+3 is starting iterator of vector> > // to be copied temp.begin()+5 is ending iterator of> > // vector to be copied> > original.insert(original.begin() + 3, temp.begin() + 2,> > temp.begin() + 5);> > > // Printing the modified vector> > cout <<> 'Vector after Inserting the portion of temp '> > 'vector in original vector :
'> ;> > for> (> auto> x : original)> > cout << x <<> ' '> ;> > return> 0;> }> > // This code contributed by Harsh Singh (hsnooob)> |
>
>산출
Original vector : 1 2 3 4 5 Vector after Inserting the portion of temp vector in original vector : 1 2 3 9 0 3 4 5>