实战c++中的vector系列--creating vector of local structure、vector of structs initialization
之前一直没有使用过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的更多相关文章
- 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)
string.vector 互转 string 转 vector vector vcBuf;string stBuf("Hello DaMao!!!");----- ...
- 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())
在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...
- 实战c++中的string系列--不要使用memset初始化string(一定别这么干)
參考链接: http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html 百度百科第一次这么给力: void *memset(voi ...
- 实战c++中的string系列--std::string与MFC中CString的转换
搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...
- 实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)
非常久没有写关于string的博客了.由于写的差点儿相同了.可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动. 在程序中,假设用到了颜色代码,一般都是十六进制的,即hex. 可是server ...
- 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)
今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...
- 实战c++中的string系列--指定浮点数有效数字并转为string
上一篇博客讲了好几种方法进行number到string的转换,这里再单独说一下float或是double到string的转换. 还是处于控件显示的原因.比方说要显示文件的大小,我们从server能够获 ...
- 实战c++中的string系列--CDuiString和string的转换(duilib中的cduistring)
使用所duilib的人定会知道cduistring类型,先看看这个类是怎么定义的: class UILIB_API CDuiString { public: enum { MAX_LOCAL_STRI ...
- 实战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 ...
随机推荐
- java中终止线程的三种方式
在java中有三种方式可以终止线程.分别为: 1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止. 2. 使用stop方法强行终止线程(这个方法不推荐使用,因为stop和sus ...
- 【Maven】1.使用myecplise配置自己的Maven配置,不使用默认的maven
[好文章]参考地址: http://www.cnblogs.com/luotaoyeah/p/3764533.html ---------------------------------------- ...
- 输入法不能使用ctrl+shift进行切换的问题
第一种情况就是,你的输入法只有一种(而且这种输入法并不是“中文(简体) 微软拼音输入法”). 如果是只有一种输入法的话,是无法进行切换的,如果你是想要把输入法切换到无输入法状态,那么你可以通过设置任务 ...
- 修改oracle内存大小
在默认安装情况下,oracle的内存分配是按系统内存的大小比例分配的,内存比较大的情况下,oracle所占的内存也大,该情况下,我们一般要修改sga值来减少系统中oracle的内存过大问题. 用dba ...
- python对文件写操作报错UnicodeEncodeError
2017-04-25 python连mongodb数据库并将提取部分数据写入本地文件时,出现UnicodeEncodeError. 解决方法:指定文件字符集为utf-8,在文件头部加入以下代码 imp ...
- ubuntu 查看系统服务的列表
service --status-all
- ECSHOP站内页面跳转,避免死链
2.x版本域名重定向: # For ISAPI_Rewrite 2.x RewriteCond Host: ^steveluo\.name$ RewriteRule (.*) http\://www\ ...
- 2017.5.27 使用propagation实现:根据参数决定是否需要事务管理
1.功能描述 要实现rest接口:POST ***/entry,其中参数中有action参数. 当action=rollback时,批量新增出错时需要回滚. 当action!=rollback时,批量 ...
- NeatBean下ssh 私钥格式问题
1. SecureCRT 生成的private key 的格式是其私有的格式, 2. 标准格式为 openssl 格式
- oracle 12C SYS,SYSTEM用户的密码都忘记或是丢失
密码 conn / as sysdba alter user system identified by Abcd1234; manual script first -->manual_scrip ...