C++实现
1、读取文件并转化为string类型
2、string切割并装入vector数组
3、string类型提取int数值
直接上代码:
#include <string>
#include <fstream>
#include <sstream>
#include <cstring>
#include <iostream>
#include<vector>
#include <stdlib.h>
using namespace std;
//从文件读入到string里
string readFileIntoString(char * filename)
{
ifstream ifile(filename);
//将文件读入到ostringstream对象buf中
ostringstream buf;
char ch;
while(buf&&ifile.get(ch))
buf.put(ch);
//返回与流对象buf关联的字符串
return buf.str();
}
//string切割放入vector数组
vector<string> vector_s(string word){
//用于存放分割后的字符串
vector<string> res;
//待分割的字符串,含有很多空格
//string word = " Hello, I want to learn C++! ";
//暂存从word中读取的字符串
string result;
//将字符串读到input中
stringstream input;
input << word;
//依次输出到result中,并存入res中
while (input >> result)