C++ string字符串提取int数值/读取文件并转化为string类型

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)
        
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一个示例代码: ``` #include <iostream> #include <fstream> #include <vector> #include <string> #include <locale> using namespace std; int main() { // 文件名 string fileName = "text.txt"; // 创建文件输入流 ifstream file(fileName); // 设置locale,支持文字符 file.imbue(locale("zh_CN.UTF-8")); // 创建字符串变量 string line; // 创建vector vector<string> chineseStrings; // 循环读取文件的每一行 while (getline(file, line)) { // 判断是否是字符串 if (line.find("[\u4e00-\u9fa5]+") != string::npos) { // 如果是,添加到vector chineseStrings.push_back(line); } } // 关闭文件 file.close(); // 输出vector的所有字符串 for (string str : chineseStrings) { cout << str << endl; } return 0; } ``` 请注意,上面的代码只是示例代码。在实际使用可能需要进行更多的错误处理和调整。 ### 回答2: 要读取一个txt文件并保存其字符串到一个vector<string>,你可以按照以下步骤进行操作: 1. 引入必要的头文件,包括iostream(输入输出流)、fstream(文件流)和vector(向量容器): ```cpp #include <iostream> #include <fstream> #include <vector> ``` 2. 创建一个函数来实现该功能: ```cpp vector<string> getChineseStringsFromTxt(const string& filePath) { vector<string> chineseStrings; ifstream file(filePath); if (file) { string line; while (getline(file, line)) { string chineseString; for (char c : line) { if (isChineseChar(c)) { //判断字符是否为文字符 chineseString += c; } } if (!chineseString.empty()) { chineseStrings.push_back(chineseString); } } file.close(); } else { cout << "无法打开文件: " << filePath << endl; } return chineseStrings; } ``` 3. 创建一个辅助函数来判断字符是否为文字符: ```cpp bool isChineseChar(char c) { return c >= -127 && c <= -2; } ``` 4. 在主函数调用该函数并输出结果: ```cpp int main() { vector<string> result = getChineseStringsFromTxt("example.txt"); for (const string& chineseString : result) { cout << chineseString << endl; } return 0; } ``` 以上是一个简单的实现方式,你可以根据自己的需求进行修改和调整。请记得首先将文本文件放在正确的位置,并传递正确的文件路径给函数。 ### 回答3: 要读取txt文件并仅将其字符串保存到vector<string>,可以按照以下步骤进行: 1. 打开txt文件:使用C++的fstream库,创建一个输入文件流对象,并使用open函数打开txt文件。例如: ```cpp #include <fstream> #include <string> #include <vector> std::ifstream file("example.txt"); if (!file.is_open()) { // 文件打开失败,处理错误情况 } ``` 2. 逐行读取文件内容:使用getline函数,循环读取文件的每一行。例如: ```cpp std::string line; while (std::getline(file, line)) { // 处理每一行的内容 } ``` 3. 提取字符串:对于每一行的内容,使用循环逐个字符判断是否为文字符。可以使用C++的ctype库的iswalpha函数来判断一个字符是否为文字符。若是文字符,则将其添加到vector<string>。例如: ```cpp std::vector<std::string> chineseStrings; for (char c : line) { if (iswalpha(c)) { std::string chCharacter(1, c); // 将字符转为字符串 chineseStrings.push_back(chCharacter); } } ``` 4. 关闭文件:使用close函数关闭打开的文件流对象。例如: ```cpp file.close(); ``` 以上就是将txt文件字符串保存到vector<string>的方法。最后,可以对vector<string>保存的字符串进行进一步处理或使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值