int &ref = value
int &ref = 10
const int x = 5;
const int &ref2 = x;
#include <iostream>
int main()
{
using namespace std;
int value1(5), value2(10);
int &ref1(value1);
cout << ref1 << endl;
ref1 = value2;
cout << ref1 << " " << value1 << endl;
return 0;
}
5
10 10
const
를 사용하여 참조변수를 선언하면 리터럴로 선언이 가능하다.const int &ref_x = 3 + 4;
void doSomething(const int &x)
{
cout << x << endl;
}
int main()
{
int a = 1;
doSomething(a);
doSomething(1);
doSomething(a + 3);
doSomething(3 * 4);
return 0;
}
1
1
4
12
struct Person
{
int age;
double weight;
};
int main()
{
Person person;
person.age = 5;
person.weight = 30;
Person &ref = person;
ref.age = 15;
ref.weight = 35;
Person *ptr = &person;
ptr->age = 30;
(*ptr).weight = 50;
Person &ref2 = *ptr;
ref.age = 45;
std::cout << &person << std::endl;
std::cout << &ref2 << std::endl;
return 0;
}
같은 메모리 주소를 출력한다.
int main()
{
int fibonacci[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
//change array values =
for(int number : fibonacci)
number = 10;
// output
for(int number : fibonacci)
cout << number << " ";
cout << endl;
//change array values
for(auto &number : fibonacci)
number *= 10;
// output
for(const auto &number : fibonacci)
cout << number << " ";
cout << endl;
int max_number = 0;
for(const auto &n : fibonacci)
max_number = std::max(max_number, n);
cout << max_number << endl;
return 0;
}
0 1 1 2 3 5 8 13 21 34 55 89
0 10 10 20 30 50 80 130 210 340 550 890
890
- 처음에 배열에 모든 수를 10으로 바꾸려고 했지만, 참조해서 배열을 가져간 것이 아니라 새로 복사를 해서 바꾼것이기 때문에 출력하면 원래 메모리에 있던 fibonacci 배열이 그대로 출력된다.
- 두번째는 fibonacci 배열의 메모리 주소를 참조하여 값을 바꾸기 때문에 10이 곱해진 수가 나온다.
- 세번째는 최대값 구하는 라이브러리를 사용하였다. (algorithm max)
enum Type
{
INT,
FLOAT,
CHAR,
};
int main()
{
int i = 5;
float f = 3.0;
char c = 'a';
void *ptr = nullptr;
ptr = &c;
ptr = &i;
ptr = &f;
Type type = FLOAT;
cout << &f << " " << ptr << endl;
if(type == FLOAT)
cout << *static_cast<float*>(ptr) << endl;
else if(type == INT)
cout << *static_cast<int*>(ptr) << endl;
return 0;
}
int main() {
using namespace std;
int *ptr = nullptr;
int **ptrptr = nullptr;
int value = 5;
ptr = &value;
ptrptr = &ptr;
cout << ptr << " " << *ptr << " " << &ptr << endl;
cout << ptrptr << " " << *ptrptr << " " << &ptrptr << endl;
cout << **ptrptr << endl;
return 0;
}
-주소 확인-
0x7ffeefbff3d4 5 0x7ffeefbff3e0
0x7ffeefbff3e0 0x7ffeefbff3d4 0x7ffeefbff3d8
5
#include <iostream>
#include <array>
using namespace std;
void printLength(const std::array<int, 5>& my_arr)
{
cout << my_arr.size() << endl;
}
int main()
{
//int array[] = { 1, 2, 3, 4, 5 };
std::array<int, 5> my_arr = { 1, 2, 3, 4, 5 };
//갯수가 적어도는 되지만 많으면 안됨
my_arr = { 0, 1, 2, 3, 4};
my_arr = { 0, 1, 2 };
cout << my_arr[0] << endl;
cout << my_arr.at(0) << endl;
printLength(my_arr);
return 0;
}
#include <iostream>
#include <array>
#include <algorithm>
using namespace std;
void printLength(const std::array<int, 5>& my_arr)
{
for (auto &element : my_arr)
cout << element << " ";
cout << endl;
}
int main()
{
//int array[] = { 1, 2, 3, 4, 5 };
std::array<int, 5> my_arr = { 21, 2, 312, 40, 5 };
printLength(my_arr);
std::sort(my_arr.begin(), my_arr.end());
printLength(my_arr);
std::sort(my_arr.rbegin(), my_arr.rend());
printLength(my_arr);
return 0;
}
#include <iostream>
#include <vector>
int main()
{
using namespace std;
// std::array<int, 5> array;
std::vector<int> array;
std::vector<int> array2 = { 1, 2, 3, 4, 5 };
cout << array2.size() << endl;
std::vector<int> array3 = { 1, 2, 3, };
cout << array3.size() << endl;
// uniform initialization
std::vector<int> array4 { 1, 2, 3, };
cout << array4.size() << endl;
vector<int> arr = {1 ,2, 3, 4, 5 };
arr.resize(10);
for(auto &itr : arr)
cout << itr << " ";
cout << endl;
cout << arr[1] << endl;
cout << arr.at(1) << endl;
return 0;
}