작은 걸음 VI — C++로 성장하기
Small 없이 인사하기
Small 없이 인사하기
이번에 배울 것
이미 배운 표준 도구만 사용하면 Small 없이도 같은 일을 할 수 있습니다.
실행해 보기
예제 · example1.cpp
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Name: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "\n";
return 0;
}
small_main 대신 main, String 대신 std::string, input 대신 getline, print 대신 cout을 씁니다. Alex를 입력하면 Hello, Alex가 나옵니다.
Small 기능을 쓰지 않으므로 small.h나 initialize_small, shutdown_small도 필요 없습니다.
Exercise — Small 없는 합계
Small API 없이 std::vector<int>의 값을 모두 더해 std::cout으로 출력하는 main 프로그램을 작성하세요.
연습 문제 코드 · exercise1_starter.cpp
#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers = {10, 20, 30};
int total = 0;
// sum and print using only standard C++.
return 0;
}
Hint
#include <iostream>과 #include <vector>를 직접 적으세요.
풀이 보기
#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers = {10, 20, 30};
int total = 0;
for (int value : numbers)
total = total + value;
std::cout << total << "\n";
return 0;
}