今天继续写一些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…
使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last. #include <iostream> // std::cout #inclu…
之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: #include <iostream> #include <vector> int main () { std::vector<int> myvector (3,100); std::vector<int>::iterator it; it = myvector…
关于vector已经写的差不多了,似乎要接近尾声了,从初始化到如何添加元素再到copy元素都有所涉及,是时候谈一谈内存的释放了. 是的,对于数据量很小的vector,完全没必要自己进行主动的释放,因为那样对程序的效率几乎没有影响.但是当vector中存入大量的数据后,并且都数据进行了一些操作,比如删除后,如果我们能积极主动的去释放内存,那么是非常明智的. 写到这里,应该明确了size和capacity的区别了. 现在介绍一个方法,std::vector::clear() Removes all…
在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 =…
jdk源码学习之String,手动实现一个String package com.amazing.jdk.string_2017_12_31; import java.io.Serializable; import java.util.Arrays; /** * Created by yaming on 17-12-31. */ public final class MyString implements Serializable{ private final char value[];//数组…
上一篇博客说道vector中放入struct.我们先构造一个struct对象.再push_back. 那段代码中,之所以不能使用emplace_back,就是由于我们定义的struct没有显示的构造函数. emplace和解? 放列的意思. 这次我们不把struct当做vector的元素了.我们把一个class当做vector的元素,写下代码: #include <iostream> #include <vector> #include<string> using na…
stl算法中有个copy函数.我们能够轻松的写出这种代码: #include <iostream> #include <algorithm> #include <vector> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { double darray[10]={1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9}; vector<double> vdoubl…
之前一直没有使用过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 =…
上篇将了对于struct或是class为何emplace_back要优越于push_back,可是另一些细节没有提及.今天就谈一谈emplace_back造成的引用失效. 直接撸代码了: #include <vector> #include <string> #include <iostream> using namespace std; int main() { vector<int> ivec; ivec.emplace_back(1); ivec.em…