惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
博客园 - Franky
IT之家
IT之家
V
Visual Studio Blog
博客园 - 【当耐特】
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
博客园 - 叶小钗
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
罗磊的独立博客
小众软件
小众软件
Jina AI
Jina AI

博客园 - 今夜太冷

GPG(GnuPG)入门 Session variables lost after the call of Response.Redirect method c++中POD类型和non-POD类型 关于c++ template的branching和Recursion的一段很好的描述 How do I remove a particular element from an array in JavaScript? Get the client's IP address in socket.io 前端 使用 crypto-js 对数据进行对称加密 C++ delegate的几种方法 MFC更换窗口图标 boost::make_function_output_iterator报错: C4996 How to copy the contents of std::vector to c-style static array,safely? std::vector push_back报错Access violation Structured Exception Handling Catch a Memory Access Violation in C++ Windows上的字符转换之CP_ACP和CP_OEMCP MFC中使用ATL报错:error C4430: missing type specifier - int assumed. Note: C++ does not support default-int C++ WINDOWS下 wchar_t *和char * 相互转化总结篇 VS2008 编译出错 fatal error C1859: unexpected precompiled header error, simply rerunning the compiler might fix this problem 解析XML出错,无法创建DOMDocument对象
Initialize a vector in C++ (5 different ways)
今夜太冷 · 2018-08-21 · via 博客园 - 今夜太冷

https://www.geeksforgeeks.org/initialize-a-vector-in-cpp-different-ways/

Following are different ways to create and initialize a vector in C++ STL

Initializing by one by one pushing values :

#include <bits/stdc++.h>

using namespace std;

int main()

{

    vector<int> vect;

    vect.push_back(10);

    vect.push_back(20);

    vect.push_back(30);

    for (int x : vect)

        cout << x << " ";

    return 0;

}

Output:

10 20 30

Specifying size and initializing all values :

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int n = 3;

    vector<int> vect(n, 10);

    for (int x : vect)

        cout << x << " ";

    return 0;

}

Output:

10 10 10

Initializing like arrays :

#include <bits/stdc++.h>

using namespace std;

int main()

{

    vector<int> vect{ 10, 20, 30 };

    for (int x : vect)

        cout << x << " ";

    return 0;

}

Output:

10 20 30

Initializing from array :

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int arr[] = { 10, 20, 30 };

    int n = sizeof(arr) / sizeof(arr[0]);

    vector<int> vect(arr, arr + n);

    for (int x : vect)

        cout << x << " ";

    return 0;

}

Output:

10 20 30

Initializing from another vector :

#include <bits/stdc++.h>

using namespace std;

int main()

{

    vector<int> vect1{ 10, 20, 30 };

    vector<int> vect2(vect1.begin(), vect.end());

    for (int x : vect2)

        cout << x << " ";

    return 0;

}

Output:

10 20 30