반복자
#include <iostream>
#include <vector>
using namespace std;
void printVector(const vector<int> v) {
cout << "The vector elements are: ";
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}
int main() {
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2;
for (int i = 0; i < vect1.size(); i++) {
vect2.push_back(vect1[i]);
}
cout << "Old vector elements are : ";
printVector(vect1);
cout << "New vector elements are : ";
printVector(vect2);
vect1[0] = 2;
cout << "The first element of old vector is :";
cout << vect1[0] << endl;
cout << "The first element of new vector is :";
cout << vect2[0] <<endl;
return 0;
}
출력
Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1
할당 연산자 =(Assignment Operator =)
#include <iostream>
#include <vector>
using namespace std;
void printVector(const vector<int> v) {
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}
int main() {
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2;
vect2 = vect1;
cout << "Old vector elements are : ";
printVector(vect1);
cout << "New vector elements are : ";
printVector(vect2);
vect1[0] = 2;
cout << "The first element of old vector is :";
cout << vect1[0] << endl;
cout << "The first element of new vector is :";
cout << vect2[0] <<endl;
return 0;
}
출력
Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1
벡터를 생성자로 전달하기(Passing Vector as Constructor)
#include <iostream>
#include <vector>
using namespace std;
void printVector(const vector<int> v) {
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}
int main() {
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2(vect1);
cout << "Old vector elements are : ";
printVector(vect1);
cout << "New vector elements are : ";
printVector(vect2);
vect1[0] = 2;
cout << "The first element of old vector is :";
cout << vect1[0] << endl;
cout << "The first element of new vector is :";
cout << vect2[0] <<endl;
return 0;
}
출력
Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1
내장 함수(Inbuilt Functions)
copy(first_iterator_o, last_iterator_o, back_inserter());
#include <iostream>
#include <vector>
using namespace std;
void printVector(const vector<int> v) {
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}
int main() {
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2;
copy(vect1.begin(), vect1.end(), back_inserter(vect2));
cout << "Old vector elements are : ";
printVector(vect1);
cout << "New vector elements are : ";
printVector(vect2);
vect1[0] = 2;
cout << "The first element of old vector is :";
cout << vect1[0] << endl;
cout << "The first element of new vector is :";
cout << vect2[0] <<endl;
return 0;
}
출력
Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1
assign(first_iterator_o, last_iterator_o);
#include <iostream>
#include <vector>
using namespace std;
void printVector(const vector<int> v) {
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}
int main() {
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2;
vect2.assign(vect1.begin(), vect1.end());
cout << "Old vector elements are : ";
printVector(vect1);
cout << "New vector elements are : ";
printVector(vect2);
vect1[0] = 2;
cout << "The first element of old vector is :";
cout << vect1[0] << endl;
cout << "The first element of new vector is :";
cout << vect2[0] <<endl;
return 0;
}
출력
Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1
'프로그래밍 언어 > C++' 카테고리의 다른 글
[C++] 원이 겹치는지에 대한 개인 코드 (0) | 2024.05.29 |
---|---|
[C++] 2차원 vector 크기 동적으로 재설정 (0) | 2024.05.08 |
[C/C++] 부동소수점의 비교연산 안되는 이유 및 오차범위 해결하기 (0) | 2024.05.03 |
[C++] 구조체 바이트 패딩 규칙 코드 (structure byte padding or align rule) (0) | 2024.04.24 |
C++ 23 추가된 주요 기능들 (0) | 2024.02.27 |