Quick Answer
Optimal vector initialization for performance and readability.
Understanding the Issue
Different initialization methods serve different purposes - from list initialization to move semantics. Choosing the right approach impacts performance.
The Problem
This code demonstrates the issue:
Cpp
Error
// Problem: C-style initialization
int arr[] = {1,2,3};
vector<int> v(arr, arr+3); // Old-style
The Solution
Here's the corrected code:
Cpp
Fixed
// Solution 1: Direct list init (C++11)
vector<int> v1 = {1, 2, 3};
// Solution 2: Fill constructor
vector<int> v2(5, 100); // 5 elements of 100
Key Takeaways
Prefer modern initialization styles for cleaner code.