vector 、map 、iterator 之學習筆記

由于本人要接手一項C++方面 的工作。由于不會C++,不過做過JAVA 以及一些web方面的開發,加之時間比較短。所以需要速成,于是學習筆記也基本都是用代碼代替。

//范例資源文件
/*****************************************************************************************
Test.txt
:
tom 123456789
lilei 234567891
zhangsan 345678912
tom 456789123
xiaohe 567891234????????????????????????????????????????????????????????????????????
******************************************************************************************/
//未用名字空間,使用vector 進行遍歷
/******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
int _tmain(int argc,_TCHAR* argv[])
{
std::string strtmp;
std::ifstream fs("Test.txt");
std::vector<std::string> vect;
while(getline(fs,strtmp,'\n'))
{
vect.push_back(strtmp.substr(0,strtmp.find(" ")));
}
for(std::vector<std::string>::size_type index = 0; index < vect.size();index++)
{
std::cout << vect[index]?? << std::endl;
}
return 0;
}
******************************************************************************************/
//使用名字空間,使用vector進行遍歷
/******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int _tmain(int argc,_TCHAR* argv[])
{
string strtmp;
ifstream fs("Test.txt");
vector<string> vect;
while(getline(fs,strtmp,'\n'))
{
vect.push_back(strtmp.substr(0,strtmp.find(" ")));
}
for(int index = 0; index < vect.size();index++)
{
cout << vect[index]?? << endl;
}
return 0;
}
******************************************************************************************/
//利用迭代器,對vector進行遍歷
/******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int _tmain(int argc,_TCHAR* argv[])
{
?string strtmp;
?ifstream fs("Test.txt");
?vector<string> vect;
?while(getline(fs,strtmp,'\n'))
?{
??vect.push_back(strtmp.substr(0,strtmp.find(" ")));
?}
?vector<string>::iterator it = vect.begin();

?for(;it != vect.end();it++)
?{
??cout << *it? << endl;
?}
?return 0;
}
******************************************************************************************/
//使用map 進行遍歷
/******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <map>

using namespace std;

int _tmain(int argc,_TCHAR* argv[])
{
?string strtmp;
?ifstream fs("Test.txt");
?map<string,string>? map_temp;
?string::size_type index = string::npos;

?while(getline(fs,strtmp,'\n'))
?{
?index = strtmp.find("");
?map_temp[strtmp.substr(0,index)] = (strtmp.substr(++index));
?}
?map<string,string> ::iterator it = map_temp.begin();

?for(; it != map_temp.end(); it++)
?{
??cout << it->first << " = " << it->second << endl;
?}
?return 0;
}
******************************************************************************************/
//使用for_each 利用重載操作符 進行map遍歷
/******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <map>
#include <iterator>
#include <algorithm>

using namespace std;
class showIiem
{
public:
void operator()(const map<string,string>::value_type& value)
{
cout << value.first << " = " << value.second << "\n";
};

};
int _tmain(int argc,_TCHAR* argv[])
{
?string strtmp;
?ifstream fs("Test.txt");
?map<string,string>? map_temp;
?string::size_type index = string::npos;

?while(getline(fs,strtmp,'\n'))
?{
??index = strtmp.find("");
??map_temp[strtmp.substr(0,index)] = (strtmp.substr(++index));
?}
?showIiem show;
?for_each(map_temp.begin(),map_temp.end(),show);
?return 0;
}
******************************************************************************************/
// 利用copy,使用重載操作符 進行map遍歷
/******************************************************************************************
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;
namespace std
{
?std::ostream& operator<<(std::ostream& os, const std::map<std::string,std::string>::value_type& value)
?{
??os << value.first << "= " << value.second;
??return os;
?}
}
class ShowValue
{
public:
?ShowValue(std::ostream& os):m_os(os)
?{
?}

?void operator()(const std::map<std::string,std::string>::value_type& value)
?{
??m_os << value.first << "= " << value.second << "\n";
?}
?std::ostream& m_os;
};
typedef std::ostream_iterator<std::map<std::string,std::string>::value_type> ositertype;
std::ostream& operator<<(std::ostream& os, const std::map<std::string,std::string>& value)
{
?std::for_each(value.begin(), value.end(), ShowValue(os));
?return os;
}
int _tmain(int argc,_TCHAR* argv[])
{
?std::string strtmp;
?std::fstream in("Test.txt");
?std::map<std::string,std::string>? map_1;
?std::string::size_type idx = std::string::npos;
?while(std::getline(in, strtmp, '\n'))
?{
??idx = strtmp.find(' ');
??map_1[strtmp.substr(0, idx)] = strtmp.substr(++idx);
?}
?ositertype os_iter(std::cout, "\n");
?std::copy(map_1.begin(), map_1.end(), os_iter);
?return 0;
}


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/452739.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/452739.shtml
英文地址,請注明出處:http://en.pswp.cn/news/452739.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

redis的密碼驗證,及哨兵的相關配置

背景我們知道&#xff0c;redis默認是不配置密碼的&#xff0c;這就造成只要有redis的IPPort就可以無需驗證&#xff0c;登陸redis。如果恰巧你的redis是開放在公網上的&#xff0c;很容易就被******&#xff0c;獲取你的系統權限&#xff0c;經常被黑去當成了礦機。redis的安全…

tag的使用

tag True while tag:print("level")choice input("level>>>").strip() #作用是暫停 不會一直死循環if choice "quit":break #終止當前循環進入到上一層if choice "quit_all": tag False #不用一層層退出 直接退出整個…

pyecharts對于經緯度_一文帶你掌握Pyecharts地理數據可視化的方法

本文主要介紹了Pyecharts地理數據可視化&#xff0c;分享給大家&#xff0c;具體如下&#xff1a;一、Pyecharts簡介和安裝1. 簡介Echarts 是一個由百度開源的數據可視化&#xff0c;憑借著良好的交互性&#xff0c;精巧的圖表設計&#xff0c;得到了眾多開發者的認可。而 Pyth…

使用Sqlmap對dvwa進行sql注入測試(初級階段)

0.測試準備 1&#xff09;打開Kali虛擬機終端; 2&#xff09;打開靶機OWASP&#xff0c;并通過瀏覽器&#xff0c;輸入IP地址進入dvwa的主頁&#xff0c;然后選擇SQL injection進入SQL注入的測試頁面 1.獲取DVWA的url和cookie 在輸入框中輸入1&#xff0c;顯示有內容&…

什么是軟件生命周期

軟件生命周期又稱為軟件生存周期或系統開發生命周期&#xff0c;是軟件的產生直到報廢的生命周期&#xff0c;周期內有問題定義、可行性分析、總體描述、系統設計、編碼、調試和測試、驗收與運行、維護升級到廢棄等階段&#xff0c;這種按時間分程的思想方法是軟件工程中的一種…

STL中map和string, vector 用法詳解

1. map 用法詳解 std map是STL的一個關聯容器&#xff0c;它提供一對一&#xff08;其中第一個可以稱為關鍵字&#xff0c;每個關鍵字只能在map中出現一次&#xff0c;第二個可能稱為該關鍵字的值&#xff09;的數據處理能力&#xff0c;由于這個特性&#xff0c;它完成有可能…

如果備份還原SecureCRT、Xshell遠程工具遠程

因為有時候電腦操作系統要重新安裝&#xff0c;需要將遠程備份下來。或者要將遠程發給其他同事。一、如何備份還原SecureCRT遠程1、打開options-global options---general---configuration paths找到配置文件保存路徑&#xff0c;如下圖&#xff1a;2、打開C:\Users\NUC\AppDat…

Centos7 下yum安裝mysql

轉載于:https://www.cnblogs.com/nbjjy/p/9023991.html

Python協程--迭代器

0. 實現一個可以迭代的對象 1&#xff09;要想使一個對象實現迭代的功能&#xff0c;須實現__iter__和__next__方法。 2&#xff09;判斷classmate是否是可以迭代的對象&#xff1a; from collections import Iterable isinstance(classmate, Iterable)結果為True則說明是可以…

什么是敏捷開發

什么是敏捷開發&#xff1f; 敏捷開發(Agile Development)是一種以人為核心、迭代、循序漸進的開發方法。 怎么理解呢&#xff1f;首先&#xff0c;我們要理解它不是一門技術&#xff0c;它是一種開發方法&#xff0c;也就是一種軟件開發的流程&#xff0c;它會指導我們用規定的…

May 18:PHP 輸出語句

通過前面的學習了解了php的基本語法&#xff0c;今天向大家簡單介紹php的幾種輸出方式&#xff1a; 1. echo 常用的輸出語句&#xff0c;例如&#xff1a;echo helloworld&#xff01;; 2. print() 輸出語句&#xff0c;有返回值。例如&#xff1a;print(helloworld&#x…

SendMessage、PostMessage原理和源代碼詳解

本文講解SendMessage、PostMessage兩個函數的實現原理&#xff0c;分為三個步驟進行講解&#xff0c;分別適合初級、中級、高級程序員進行理解&#xff0c;三個步驟分別為&#xff1a; 1、SendMessage、PostMessage的運行機制。 2、SendMessage、PostMessage的運行內幕。 3、…

Python協程--實現斐波那契數列(Fibonacci)的幾種方式

1.使用for遍歷list數組 # 使用for遍歷list數組 nums list() a 0 b 1 i 0while i < 10:nums.append(a)a, b b, abi 1for num in nums:print(num)2.使用迭代器完成 class Fibonacci(object):def __init__(self, all_num):self.all_num all_numself.current_num 0sel…

敏捷開發宣言

敏捷開發宣言&#xff1a; 1. 個體和交互勝過過程和工具 2. 可工作的軟件勝過面面俱到的文檔 3. 客戶協作勝過合同談判 4. 響應變化勝過遵循計劃 從上面的宣言可以看出&#xff0c;敏捷開發的核心是人 、協作、時刻可運行的軟件、變化。

java fast math,Java FastMath.signum方法代碼示例

import org.apache.commons.math3.util.FastMath; //導入方法依賴的package包/類/*** {inheritDoc}*/Overrideprotected double doSolve()throws TooManyEvaluationsException,NoBracketingException {double min getMin();double max getMax();// [x1, x2] is the bracketin…

什么是可行性研究

1.并非任何問題都有簡單明顯的解決辦法&#xff0c;事實上&#xff0c;許多問題不可能在預定的系統規模或時間期限之內解決。2.如果問題沒有可行的解&#xff0c;那么花費在這項工程上的任何時間、人力、軟硬件資源和經費&#xff0c;都是無謂的浪費。3.可行性研究的目的&#…

FTP服務的簡介和配置詳解

FTP服務的簡介和配置詳解注意&#xff1a;配置FTP服務時&#xff0c;最好關閉防火墻和selinux1、FTP服務簡介FTP 是File Transfer Protocol&#xff08;文件傳輸協議&#xff09;的英文簡稱&#xff0c;而中文簡稱為“文件傳輸協議”。用于Internet上的控制文件的雙向傳輸。同時…

Python協程--生成器(通過異常來判斷生成器已經結束)

以實現斐波那契數列為例&#xff1a; def create_num(all_num):a, b 0, 1current_num 0while current_num < all_num:yield a # 如果一個函數中有yield語句&#xff0c;那么這個就不在是函數&#xff0c;而是一個生成器的模板a, b b, abcurrent_num 1return "ok.…

四種類型轉換 cast

1.static_cast 2.dynamic_cast 3.const_cast 4. reinterpret_cast 例子1&#xff1a; float x; cout<<static_cast<int>(x); ... f(static_cast<string>("hello")); 例子2&#xff1a; class Car; class Cabriolet:pbulic Car { …

java獲取不重復訂單號,Java 生成永不重復的訂單號

package com.taiping.test;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Random;public class Test12 {/*** 生成永不重復的訂單號* param startLetter 訂單號開頭字符串* param size 訂單號中隨機的大寫字母個數* return*/public static String…