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.mzph.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…