我们通过一些代码中可能遇到的情况,消化上文中的知识:
vector的遍历:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[]) {
vector<int>int_vec;
int_vec.push_back(1);
int_vec.push_back(2);
int nSize = int_vec.size();
cout << "first C [index]: ";
for (int i = 0; i < nSize; i++) {
cout << int_vec[i] << "\t";
}
cout << endl;
cout << "second C++ at(index) method: ";
for (int i = 0; i < nSize; i++) {
int& nValue = int_vec.at(i);
cout << nValue << "\t";
}
cout << endl;
cout << "third iterator while method: ";
vector<int>::iterator int_vec_iter = int_vec.begin();
while (int_vec_iter != int_vec.end()) {
cout << *int_vec_iter << "\t";
int_vec_iter++;
}
cout << endl;
cout << "four iterator for method: ";
int_vec_iter = int_vec.begin();
for (; int_vec_iter != int_vec.end(); int_vec_iter++) {
cout << *int_vec_iter << ",";
}
cout << endl;
cout << "five auto for method: ";
int_vec_iter = int_vec.begin();
for (auto& p : int_vec)
cout << p << ",";
cout << endl;
cout << "six copy iterator method: ";
copy(int_vec.begin(), int_vec.end(), ostream_iterator<int>(cout, ","));
cout << endl;
cout << "seven copy iterator method: ";
for_each(int_vec.begin(), int_vec.end(), [](auto x) {cout << x << ","; });
cout << endl;
return 0;
}
元素的修改:
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> int_vec;
int_vec.push_back(1);
int_vec.push_back(2);
int_vec.push_back(3);
cout << "changed by array index[1] = 5: " ;
cout << endl;
int_vec[1] = 5;
for (int i = 0; i < int_vec.size(); i++) {
cout << int_vec[i] << ",";
}
cout << endl;
cout << "changed by & [1] = 10: " ;
int& m = int_vec.at(1);
m = 10;
cout << endl;
for (int i = 0; i < int_vec.size(); i++) {
cout << int_vec[i] << ",";
}
cout << endl;
cout << "changed by iterator [1] = 20: ";
vector<int>::iterator it = int_vec.begin() + 1;
*it = 20;
cout << endl;
for (int i = 0; i < int_vec.size(); i++) {
cout << int_vec[i] << ",";
}
cout << endl;
return 0;
}
元素的删除:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> int_vec;
for (int i = 0; i <= 10; i++) {
int_vec.push_back(i);
}
for (int i = 0; i < int_vec.size(); i++) {
cout << int_vec[i] << ",";
}
cout << endl;
cout << "erase the 5th: " << endl;
int_vec.erase(int_vec.begin() + 4);
for (int i = 0; i < int_vec.size(); i++) {
cout << int_vec[i] << ",";
}
cout << endl;
cout << "erase the 2th-5th: " << endl;
int_vec.erase(int_vec.begin() + 1, int_vec.begin() + 5);
for (int i = 0; i < int_vec.size(); i++) {
cout << int_vec[i] << ",";
}
return 0;
}
代码改进:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> int_vec;
for (int i = 0; i <= 10; i++) {
int_vec.push_back(i);
}
for (int i = 0; i < int_vec.size(); i++) {
cout << int_vec[i] << ",";
}
cout << endl;
cout << "erase the 5th: " << endl;
int_vec.erase(int_vec.begin() + 4);
for (int i = 0; i < int_vec.size(); i++) {
cout << int_vec[i] << ",";
}
cout << endl;
cout << "erase the 2th-5th: " << endl;
int_vec.erase(int_vec.begin() + 1, int_vec.begin() + 5);
for (int i = 0; i < int_vec.size(); i++) {
cout << int_vec[i] << ",";
}
cout << endl;
cout << "after erase if we use index to show detail: " << endl;
for (int i = 0; i <= 10; i++) {
cout << int_vec[i] << ",";
}
cout << endl;
return 0;
}
最后我们通过一个实例将vector与oop代码结合:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student {
private:
string m_strNO;
string m_strName;
string m_strSex;
string m_strDate;
public:
Student(const string& strNo,const string& strName,const string& strSex,const string& strDate)
:m_strNO(strNo),m_strName(strName),m_strSex(strSex),m_strDate(strDate){}
friend class StudCollect;
void display() {
cout << m_strNO << "," << m_strName << "," << m_strSex << "," << m_strDate << endl;
}
};
class StudCollect {
private:
vector<Student>m_vStud;
public:
void Add(const Student& s) { //拷贝s
m_vStud.push_back(s);
}
Student* Find(const string& strNO) { //以引用的方式传递string
bool bFind = false;
int i = 0;
for (; i < m_vStud.size(); i++) {
Student& s = m_vStud.at(i); //本身
if (s.m_strNO == strNO) {
bFind = true;
break;
}
}
Student* s = nullptr; //nullptr c++11 空指针
if (bFind)
s = &m_vStud.at(i);
return s;
}
};
int main() {
Student s1("1001", "Jon", "boy", "1985-10-10");
Student s2("1002", "Anna", "girl", "1984-6-10");
Student s3("1003", "Make", "boy", "1985-11-15");
StudCollect s;
s.Add(s1); s.Add(s2); s.Add(s3);
Student* ps = s.Find("1002");
if (ps) {
ps->display();
}
return 0;
}
那么按照c++ 编程思想,使用STL怎么写呢?
#include <iostream>
#include <vector>
#include <string>
#include <functional>
using namespace std;
class Student {
private:
string m_strNO;
string m_strName;
string m_strSex;
string m_strDate;
public:
Student(const string& strNo, const string& strName, const string& strSex, const string& strDate)
:m_strNO(strNo), m_strName(strName), m_strSex(strSex), m_strDate(strDate) {}
friend class StudFind;
void display() {
cout << m_strNO << "," << m_strName << "," << m_strSex << "," << m_strDate << endl;
}
};
class StudFind {
string NO;
public:
StudFind(string NO) :NO(NO) {}
bool operator()(const Student& s) {
return s.m_strNO == NO;
}
};
int main() {
Student s1("1001", "Jon", "boy", "1985-10-10");
Student s2("1002", "Anna", "girl", "1984-6-10");
Student s3("1003", "Make", "boy", "1985-11-15");
vector<Student> s;
s.push_back(s1); s.push_back(s2); s.push_back(s3);
vector<Student>::iterator it = find_if(s.begin(), s.end(), StudFind("1003"));
if (it != s.end()) {
it->display();
}
return 0;
}