之前一直没有使用过vector<struct>,如今就写一个简短的代码:

#include <vector>
#include <iostream>
int main() {
struct st { int a; };
std::vector<st> v;
v.resize(4);
for (std::vector<st>::size_type i = 0; i < v.size(); i++) {
v.operator[](i).a = i + 1; // v[i].a = i+1;
} for (int i = 0; i < v.size(); i++)
{
std::cout << v[i].a << std::endl;
}
}

用VS2015编译成功。执行结果:

1

2

3

4

可是,这是C++11之后才同意的,之前编译器并不同意你写这种语法。不同意vector容器内放local structure。

更进一步,假设struct里面有好几个字段呢?

#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct subject {
string name;
int marks;
int credits;
}; int main() {
vector<subject> sub; //Push back new subject created with default constructor.
sub.push_back(subject()); //Vector now has 1 element @ index 0, so modify it.
sub[0].name = "english"; //Add a new element if you want another:
sub.push_back(subject()); //Modify its name and marks.
sub[1].name = "math";
sub[1].marks = 90; sub.push_back({ "Sport", 70, 0 });
sub.resize(8);
//sub.emplace_back("Sport", 70, 0 ); for (int i = 0; i < sub.size(); i++)
{
std::cout << sub[i].name << std::endl;
} }

可是上面的做法不好。我们应该先构造对象。后进行push_back 或许更加明智。

subject subObj;

subObj.name = s1;

sub.push_back(subObj);

这个就牵扯到一个问题,为什么不使用emplace_back来替代push_back呢,这也是我们接下来讨论 话题。

实战c++中的vector系列--creating vector of local structure、vector of structs initialization的更多相关文章

  1. 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)

    string.vector 互转 string 转 vector vector  vcBuf;string        stBuf("Hello DaMao!!!");----- ...

  2. 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())

    在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...

  3. 实战c++中的string系列--不要使用memset初始化string(一定别这么干)

    參考链接: http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html 百度百科第一次这么给力: void *memset(voi ...

  4. 实战c++中的string系列--std::string与MFC中CString的转换

    搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...

  5. 实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)

    非常久没有写关于string的博客了.由于写的差点儿相同了.可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动. 在程序中,假设用到了颜色代码,一般都是十六进制的,即hex. 可是server ...

  6. 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)

    今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...

  7. 实战c++中的string系列--指定浮点数有效数字并转为string

    上一篇博客讲了好几种方法进行number到string的转换,这里再单独说一下float或是double到string的转换. 还是处于控件显示的原因.比方说要显示文件的大小,我们从server能够获 ...

  8. 实战c++中的string系列--CDuiString和string的转换(duilib中的cduistring)

    使用所duilib的人定会知道cduistring类型,先看看这个类是怎么定义的: class UILIB_API CDuiString { public: enum { MAX_LOCAL_STRI ...

  9. 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

    使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...

随机推荐

  1. sqlserver 下载地址(SQL Server 2008 R2 中英文 开发版/企业版/标准版 下载)

    转自:http://blog.sina.com.cn/s/blog_624b1f950100pioh.html   注:企业版无法安装在xp和win7,开发版才可以! 一. 简体中文 1. SQL S ...

  2. PS Tips: powershell 将文件以utf8格式打开并另存

    1. powershell 将文件以utf8格式打开并另存 Get-Content .\rep_position_liquidity.csv -Encoding UTF8 | Set-Content ...

  3. 怎对于Foreach 不能添加IF的问题

    我不们直接在Foreach 里面直接添加IF,这样会报错,这个前提是子视图,其他的我没有试验过.像这样: @foreach (Gift.Modules.Model.Entitys.XT_CZ item ...

  4. JAVA常见算法题(二十一)

    package com.xiaowu.demo; //求1+2!+3!+...+20!的和. public class Demo21 { public static void main(String[ ...

  5. Resin服务器部署web项目

    Resin服务器部署web项目 学习了:https://blog.csdn.net/eff666/article/details/53324167 需要配置resin.xml文件: <host ...

  6. Spring管理的bean初始化方法的三种方式,以及@PostConstruct不起作用的原因

    1:Spring 容器中的 Bean 是有生命周期的,spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作.下面是常用的三种指定特定操作的方法: 通过实现InitializingBea ...

  7. UVa 156 Ananagrams(STL,map)

     Ananagrams  Most crossword puzzle fans are used to anagrams--groups of words with the same letters ...

  8. WEB接口测试之Jmeter接口测试自动化 (二)(数据分离)

    转载:    http://www.cnblogs.com/chengtch/p/6105231.html 通过逐个录入的方式,好不容易将需要测试几十个接口的300多个测试用例录入sampler-ht ...

  9. 模拟select控件&&显示单击的坐标&&用户按下键盘,显示keyCode

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 创建你的第一个ionic+cordova应用(1)

    前面我们安装了前端的神器webstorm11,体验到了强大的开发体验,接着我们来安装ionic 必备: Node.js (npm安装工具) 百度下载 官网下载  注:如果官网新版不能安装请用百度下载0 ...