소개
현대 C++ 프로그래밍에서 벡터를 생성하고 초기화하는 작업은 더욱 간결하고 직관적이게 되었습니다. 이 튜토리얼에서는 초기화 목록을 사용하여 벡터를 초기화하는 다양한 기술을 탐구하여 개발자들이 벡터 컨테이너를 효율적으로 채우는 강력하고 간결한 방법을 제공합니다.
현대 C++ 프로그래밍에서 벡터를 생성하고 초기화하는 작업은 더욱 간결하고 직관적이게 되었습니다. 이 튜토리얼에서는 초기화 목록을 사용하여 벡터를 초기화하는 다양한 기술을 탐구하여 개발자들이 벡터 컨테이너를 효율적으로 채우는 강력하고 간결한 방법을 제공합니다.
C++ 에서 벡터는 동적 배열로, 유연한 메모리 관리와 강력한 초기화 기법을 제공합니다. 현대 C++ 에서 효율적인 프로그래밍을 위해 벡터 초기화를 이해하는 것은 필수적입니다.
std::vector<int> emptyVector; // 빈 벡터 생성
std::vector<int> sizedVector(5); // 5 개 요소를 갖는 벡터 생성, 모두 0 으로 초기화
std::vector<int> prefilledVector(5, 10); // 5 개 요소를 갖는 벡터 생성, 모두 10 으로 설정
std::vector<int> initializerListVector = {1, 2, 3, 4, 5}; // 직접 초기화
std::vector<int> anotherVector{1, 2, 3, 4, 5}; // 균일 초기화
std::vector<int> originalVector = {1, 2, 3};
std::vector<int> copiedVector(originalVector); // originalVector 의 복사본 생성
| 초기화 방법 | 성능 | 메모리 오버헤드 |
|---|---|---|
| 빈 벡터 | 낮음 | 최소 |
| 크기 지정 | 중간 | 예측 가능 |
| 초기화 목록 | 높음 | 직접적 |
#include <vector>
#include <iostream>
int main() {
// 최신 벡터 초기화 기법
std::vector<int> dynamicVector = {10, 20, 30, 40, 50};
for(int value : dynamicVector) {
std::cout << value << " ";
}
return 0;
}
C++ 에서 벡터 초기화는 개발자들에게 컨테이너를 생성하고 채우는 다양하고 효율적인 방법을 제공하여 더욱 표현력 있고 간결한 코드를 작성할 수 있도록 합니다.
초기화 목록은 현대 C++ 에서 컨테이너와 객체를 초기화하는 강력하고 간결한 방법을 제공합니다. 데이터 구조를 쉽게 생성하고 채우는 유연한 메커니즘을 제공합니다.
std::vector<int> numbers = {1, 2, 3, 4, 5};
std::vector<std::string> fruits {"apple", "banana", "cherry"};
std::vector<double> mixedValues = {1.1, 2.2, 3.3, 4.4};
std::vector<std::pair<int, string>> complexVector = {{1, "one"}, {2, "two"}};
| 초기화 유형 | 메모리 효율성 | 성능 |
|---|---|---|
| 직접 초기화 | 높음 | 빠름 |
| 복사 초기화 | 중간 | 보통 |
| 이동 초기화 | 최적 | 가장 빠름 |
class Person {
public:
Person(std::initializer_list<std::string> names) {
for(const auto& name : names) {
// 이름 처리
}
}
};
Person team = {"Alice", "Bob", "Charlie"};
#include <vector>
#include <iostream>
void processInitializerList(std::initializer_list<int> list) {
for(int value : list) {
std::cout << value << " ";
}
}
int main() {
processInitializerList({10, 20, 30, 40, 50});
return 0;
}
auto dynamicList = {1, 2, 3, 4, 5}; // std::initializer_list<int>
C++ 의 초기화 목록은 컨테이너와 객체를 초기화하는 현대적이고 타입 안전하며 표현력 있는 방법을 제공하여 코드 가독성과 유연성을 향상시킵니다.
class GradeBook {
private:
std::vector<int> studentGrades;
public:
GradeBook(std::initializer_list<int> grades) : studentGrades(grades) {}
double calculateAverage() {
double total = std::accumulate(studentGrades.begin(), studentGrades.end(), 0.0);
return total / studentGrades.size();
}
};
GradeBook mathClass = {85, 92, 78, 90, 88};
struct DatabaseConfig {
std::vector<std::string> hosts;
std::vector<int> ports;
};
DatabaseConfig config = {
{"localhost", "127.0.0.1"},
{5432, 8080}
};
std::vector<int> originalNumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> evenNumbers;
std::copy_if(originalNumbers.begin(), originalNumbers.end(),
std::back_inserter(evenNumbers),
[](int n) { return n % 2 == 0; });
| 초기화 방법 | 메모리 사용량 | 초기화 속도 |
|---|---|---|
| 직접 목록 | 낮음 | 빠름 |
| 프로그래밍 방식 | 중간 | 보통 |
| 동적 할당 | 높음 | 느림 |
class GameCharacter {
private:
std::vector<std::string> skills;
std::vector<int> attributes;
public:
GameCharacter(std::initializer_list<std::string> characterSkills,
std::initializer_list<int> characterAttributes)
: skills(characterSkills), attributes(characterAttributes) {}
};
GameCharacter warrior = {
{"Sword Strike", "Shield Block"},
{90, 85, 75}
};
template<typename T>
class DataProcessor {
private:
std::vector<T> data;
public:
DataProcessor(std::initializer_list<T> initData) : data(initData) {}
std::vector<T> filterData(std::function<bool(T)> predicate) {
std::vector<T> result;
std::copy_if(data.begin(), data.end(),
std::back_inserter(result), predicate);
return result;
}
};
DataProcessor<int> processor = {10, 20, 30, 40, 50};
실제 벡터 초기화 기법은 현대 C++ 컨테이너 관리의 강력함과 유연성을 보여주며, 개발자가 더욱 표현력 있고 효율적인 코드를 작성할 수 있도록 지원합니다.
C++ 에서 초기화 목록을 사용하여 벡터를 초기화하는 방법을 숙달함으로써 개발자는 더욱 표현력 있고 읽기 쉬운 코드를 작성할 수 있습니다. 이러한 기법은 벡터 생성을 단순화하고, 반복적인 코드를 줄이며, 현대 C++ 언어 기능을 활용하여 더욱 우아하고 성능이 좋은 데이터 구조 관리를 가능하게 합니다.