实战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呢,这也是我们接下来讨论 话题。

时间: 2024-12-28 00:47:21

实战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!!!");----------------------------------------------vcBuf.resize(stBuf.size());vcBuf.assign(stBuf.begin(), stBuf.end()); vector 转 string  stBuf.clear();stBuf.assign(v

实战c++中的string系列--std:vector&lt;char&gt; 和std:string相互转换(vector to stringstream)

有时候也会遇到std:vector与转std:string 相互转换的情况. 首先看一下vector<char>如何转string: std::vector<char> *data = response->getResponseData(); std::string res; //方法一 for (int i = 0;i<data->size();++i) { res+=(*data)[i]; } res+='\0'; std:cout << res;

实战c++中的string系列--string的分割、替换(类似string.split或是explode())

对一个字符串根据某个字符进行分割也是在实战中经常遇到的问题,也是面试中经常会被人提及的. 如果你是个C Sharp程序员,你会知晓string.split函数,有下面这些重载: 1) public string[] Split(params char[] separator) 2) public string[] Split(char[] separator, int count) 3) public string[] Split(char[] separator, StringSplitOpt

实战c++中的string系列--string的遍历(使用下标还是iterator)

迭代器提供了访问容器中对象的方法.例如,可以使用一对迭代器指定list或vector中的一定范围的对象.迭代器就如同一个指针.事实上,C++的指针也是一种迭代器.但是,迭代器也可以是那些定义了operator*()以及其他类似于指针的操作符地方法的类对象. 我们都知道可以用下标运算来访问string对象和vector对象.而另外还有一种更通用的方法也可以实现这样的方法.名曰:迭代器(iterator). 类似于指针,迭代器也提供了对对象的间接访问.就迭代器而言,其对象是容器中的元素或者strin

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

在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针, 内容与本string串同样. 这就看到了吧,返回值是const char*,这里须要注意一下. 1 string转const char* 当然是用到上面所述的方法c_str(): string s1 = "abcdeg"; const char *k =

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

非常久没有写关于string的博客了.由于写的差点儿相同了.可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动. 在程序中,假设用到了颜色代码,一般都是十六进制的,即hex. 可是server给你返回一个颜色字符串.即hex string 你怎么把这个hex string 转为 hex,并在你的代码中使用? 更进一步,你怎么办把一个形如"#ffceed"的hex string 转为 RGB呢? 第一个问题在Java中是这样搞的: public static int parseC

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

搞过MFC的人都知道cstring,给我们提供了很多便利的方法. CString 是一种很有用的数据类型.它们很大程度上简化了MFC中的许多操作,使得MFC在做字符串操作的时候方便了很多.不管怎样,使用CString有很多特殊的技巧,特别是对于纯C背景下走出来的程序员来说有点难以学习. 但是很多情况下,我们还是需要cstring和string的转换. 分两步: 1把cstring转为char数组 2根据char数组,构造自己的string(记得释放内存) std::string CStringT

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

今天继续写一些string操作. string给我们提供了很多的方法,但是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\program files\csdn",但是我想得到"D:\program files\vagaa"这个路径. 这就需要字符串的替换 std::string full_path = "D:\\program files\\csdn" const size_t last_

实战c++中的string系列--函数返回局部变量string(引用局部string,局部string的.c_str()函数)

当函数返回字符串的时候,我们可以定义返回string和string&. 1写一个返回string引用的函数 std::string & TestStringReference() { std::string loal_str = "holy shit"; return loal_str; } 这个函数当然是错误的,编译器会提示我们: 返回局部变量或临时变量的地址: loal_str 即不能返回局部变量的引用. 2写一个返回string的函数(函数返回局部变量string