網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
首先,要準(zhǔn)備好words.txt(英文文章)置于工程目錄下
思路:
1.打開(kāi)文件
2.讀取每一行
3.找到特殊的標(biāo)點(diǎn)符號(hào)的位置,進(jìn)行刪除。
4.根據(jù)空格截取單詞 find(" ");
5.將拿到的每一個(gè)單詞放在鏈表中
一:讀取一行,去除該行標(biāo)點(diǎn)符號(hào)
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#include<list>
void test_word_split();
int main()
{
test_word_split();
return 0;
}
void test_word_split()
{
fstream fs;
char filename[20] = {0};
cout<<"請(qǐng)輸入打開(kāi)的文件名:";
cin>>filename;
//打開(kāi)文件
fs.open(filename);
cout<<"打開(kāi)成功"<<filename<<endl;
char buf[1024] = {0};
fs.getline(buf,1024);//讀取每一行
cout<<buf<<endl;
size_t pos; //找到位置
string line; //接替buf職責(zé)
line = buf;
pos = line.find_first_of(",.;:'?!()/\""); //找特殊的標(biāo)點(diǎn)符號(hào)
while(pos!=string::npos)
{ //刪除單個(gè)字符
line.erase(pos,1);
//再找下一個(gè)單個(gè)的字符
pos = line.find_first_of(",.;:'?!()/\"");
}
cout<<line.c_str()<<endl; //string 轉(zhuǎn)char
}
二:截取單詞
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#include<list>
void test_word_split();
int main()
{
test_word_split();
return 0;
}
void test_word_split()
{
fstream fs;
char filename[20] = {0};
cout<<"請(qǐng)輸入打開(kāi)的文件名:";
cin>>filename;
//打開(kāi)文件
fs.open(filename);
cout<<"打開(kāi)成功"<<filename<<endl;
char buf[1024] = {0};
fs.getline(buf,1024);//讀取每一行
cout<<buf<<endl;
size_t pos;
string line,word;
line = buf;
pos = line.find_first_of(",.;:'?!()/\""); //找特殊的標(biāo)點(diǎn)符號(hào)
while(pos!=string::npos)
{ //刪除單個(gè)字符
line.erase(pos,1); //從什么位置開(kāi)始刪除多長(zhǎng)的字符
//再找下一個(gè)單個(gè)的字符
pos = line.find_first_of(",.;:'?!()/\"");
}
cout<<line.c_str()<<endl; //string 轉(zhuǎn)char
//根據(jù)空格截取單詞 find("") 111 222 333
pos = line.find(" ");
while(pos!=string::npos)
{
//截取單詞
word = line.substr(0,pos);//從0開(kāi)始,一直截到空格所在位置
cout<<word<<endl;
//把第一個(gè)單詞以及空格刪除
line.erase(0,pos+1); //從什么位置開(kāi)始刪除多長(zhǎng)的字符(如刪111 )因此pos+1
pos = line.find(" "); //尋找下一個(gè)空格
}
}
三:將拿到的每一個(gè)單詞都放在鏈表中
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#include<list>
void test_word_split();
int main()
{
test_word_split();
return 0;
}
void test_word_split()
{
list<string> wordList;//鏈表
fstream fs;
char filename[20] = {0};
cout<<"請(qǐng)輸入打開(kāi)的文件名:";
cin>>filename;
fs.open(filename);
cout<<"打開(kāi)成功"<<filename<<endl;
char buf[1024] = {0};
string line,word; //初始化定義
while(fs.getline(buf, 1024))//讀取每一行
{
size_t pos; //找到位置
line = buf; //接替buf職責(zé)
pos = line.find_first_of(",.;:'?!()/\"");
while(pos!=string::npos)//!=npos就找到
{
line.erase(pos,1); //從什么位置開(kāi)始刪除多長(zhǎng)字符
pos = line.find_first_of(",.;:'?!()/\"");//尋找下一個(gè)標(biāo)點(diǎn)符號(hào)
}
pos = line.find(" "); //尋找空格所在位置
while(pos!=string::npos)
{
word = line.substr(0,pos);//從0開(kāi)始,一直截到空格所在位置
wordList.push_back(word); //拿到的單詞放在鏈表中
//把第一個(gè)單詞以及空格刪除
line.erase(0, pos+1);//從什么位置開(kāi)始刪除多長(zhǎng)的字符(如刪111 )因此pos+1
pos = line.find(" ");//尋找下一個(gè)空格
}
}
cout<<"驗(yàn)證一下"<<endl;
list<string>::iterator it;
for(it = wordList.begin();it!=wordList.end();it++)
{
cout<<(*it).c_str()<<endl;
}
cout<<"總的個(gè)數(shù):"<<wordList.size();
fs.close();
}
最后的結(jié)果:
原文鏈接:https://blog.csdn.net/m0_56051805/article/details/124395841
相關(guān)推薦
- 2024-01-09 setFirstResult ,setMaxResults
- 2023-02-26 Docker格式化輸出命令:"docker?inspect?--format"?學(xué)習(xí)記錄_docke
- 2022-09-15 docker倉(cāng)庫(kù)登錄及配置insecure-registries的方法_docker
- 2022-12-01 Golang打印復(fù)雜結(jié)構(gòu)體兩種方法詳解_Golang
- 2022-09-15 C++關(guān)于/2和>>1的區(qū)別說(shuō)明_C 語(yǔ)言
- 2023-04-24 FFmpeg實(shí)戰(zhàn)之分離出PCM數(shù)據(jù)_C 語(yǔ)言
- 2022-12-12 C語(yǔ)言實(shí)現(xiàn)打印星號(hào)圖案_C 語(yǔ)言
- 2022-06-30 PyTorch詳解經(jīng)典網(wǎng)絡(luò)種含并行連結(jié)的網(wǎng)絡(luò)GoogLeNet實(shí)現(xiàn)流程_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支