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

推荐订阅源

Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
J
Java Code Geeks
L
LangChain Blog
V
V2EX
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
博客园 - Franky
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
博客园 - 司徒正美
B
Blog
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
Engineering at Meta
Engineering at Meta
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog

博客园 - 今夜太冷

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