Ish*_*war -1 c++ stl initialization vector memset
在数组中,我们可以执行int arr[100]={0}此操作
,这会将数组中的所有值初始化为0。我尝试使用vector like进行相同操作
vector <int> v(100)={0},但它给出了错误
error: expected ‘,’ or ‘;’ before ‘=’ token。另外,如果我们可以通过C ++的“ memset”功能执行此操作,请也告诉该解决方案。
R S*_*ahu 10
您可以使用:
std::vector<int> v(100); // 100 is the number of elements.
// The elements are initialized with zero values.
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令明确显示零值:
std::vector<int> v(100, 0);
Run Code Online (Sandbox Code Playgroud)
您可以使用第二种形式将所有元素初始化为零以外的其他值。
std::vector<int> v(100, 5); // Creates object with 100 elements.
// Each of the elements is initialized to 5
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6055 次 |
| 最近记录: |