- 이차원 벡터인 vector_2d에 vector1 과 vector2를 요소로 넣었다
- 이후 vector1 0번째 요소를 바꿨다
- vector1의 요소를 변경했으니 과연 vector_2d에도 변화가 있을까?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> vector1;
vector <int> vector2;
vector1.push_back(10);
vector1.push_back(20);
for (int i = 0; i < vector1.size(); i++)
{
cout << vector1.at(i) << endl;
}
cout << "===============" << endl;
vector2.push_back(100);
vector2.push_back(200);
for (int i = 0; i < vector2.size(); i++)
{
cout << vector2.at(i) << endl;
}
cout << "===============" << endl;
vector <vector<int>> vector_2d;
vector_2d.push_back(vector1);
vector_2d.push_back(vector2);
cout << vector_2d.at(0).at(0) << endl;
cout << vector_2d.at(0).at(1) << endl;
cout << vector_2d.at(1).at(0) << endl;
cout << vector_2d.at(1).at(1) << endl;
cout << "===============" << endl;
vector1.at(0) = 1000;
cout << vector_2d.at(0).at(0) << endl;
cout << vector_2d.at(0).at(1) << endl;
cout << vector_2d.at(1).at(0) << endl;
cout << vector_2d.at(1).at(1) << endl;
cout << "===============" << endl;
for (int i = 0; i < vector1.size(); i++)
{
cout << vector1.at(i) << endl;
}
return 0;
}
출력하면 다음과 같다.
- 벡터와 배열을 사용한 평균 구하기
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ary {13,20,30,40};
int sum {0};
for (int i = 0; i < ary.size(); i++)
{
sum = sum + ary.at(i);
}
cout << "sum : " << sum << endl;
double avr {0.0};
avr = static_cast<double>(sum) / ary.size();
cout << "avr : " << avr << endl;;
//==========================================
int ary[]{ 13,20,30,40 };
int ary_length = sizeof(ary) / sizeof(ary[0]);
cout << ary_length << endl;
int sum{};
for (int i = 0; i < ary_length; i++)
{
sum += ary[i];
}
cout << "sum : " << sum << endl;
double avr{ 0.0 };
avr = static_cast<double>(sum) / ary_length;
cout << "sum : " << sum << endl;
return 0;
}
'언어' 카테고리의 다른 글
[C++] rand() (0) | 2023.09.13 |
---|---|
[C++] cmath 라이브러리 (0) | 2023.09.13 |
[C++] find() / npos (0) | 2023.09.07 |
[C++] 2차원 배열을 X좌표 Y좌표로 생각하기! (1) | 2023.03.24 |
[C/CPP] 포인터 개념 (2) | 2023.03.17 |