使用map實(shí)現(xiàn)單詞轉(zhuǎn)換的實(shí)例分析
更新時(shí)間:2013年05月28日 15:38:28 作者:
本篇文章是對(duì)使用map實(shí)現(xiàn)單詞轉(zhuǎn)換的代碼實(shí)例進(jìn)行了纖細(xì)的分析介紹,需要的朋友參考下
使用map實(shí)現(xiàn)單詞轉(zhuǎn)換的實(shí)例分析
從map中查找單詞時(shí)必須使用find函數(shù),不能使用下表,因?yàn)樵趍ap中使用下標(biāo)訪問(wèn)不存在的元素將導(dǎo)致在map容器中添加一個(gè)新的元素,新元素的key即要查找的內(nèi)容。
/*****************************************************************************
* Open file
*****************************************************************************/
ifstream& open_file(ifstream &in, const string &file)
{
in.close(); // close in case it was already open
in.clear(); // clear any existing errors
// if the open fails, the stream will be in an invalid state
in.open(file.c_str()); // open the file we were given
return in; // condition state is good if open succeeded
}
/*****************************************************************************
* Word Transform
*****************************************************************************/
void WordTransform(const string rule, const string infile)
{
if (rule.empty() || infile.empty())
{
return;
}
map<string ,string> trans_map;
string key, value;
// Open transformation file and check that open succeeded
ifstream map_file;
if (!open_file(map_file, rule))
{
throw runtime_error("No transformation file.");
}
// Read the transformation map and build the map
while (map_file >> key >> value)
{
trans_map.insert(make_pair(key, value));
}
// Open the input file and check that the open succeeded
ifstream input;
if (!open_file(input, infile))
{
throw runtime_error("No input file.");
}
string line; // Hold each line from the input
// Read the text to transform it a line at a time
while (getline(input, line))
{
istringstream stream(line); // Read the line a word at a time
string word;
bool bFirstWordFlg = true; // Controls whether a space is printed
while (stream >> word)
{
// ok: the actual mapwork, this part is the heart of the program
map<string, string>::const_iterator map_it = trans_map.find(word);
// If this word is in the transformation map
if (map_it != trans_map.end())
{
// Replace it by the transformaion value in the map
word = map_it->second;
}
if (bFirstWordFlg)
{
bFirstWordFlg = false;
}
else
{
cout << " "; // Print space between words
}
cout << word;
}
cout << endl; // Done with this line of input
}
}
從map中查找單詞時(shí)必須使用find函數(shù),不能使用下表,因?yàn)樵趍ap中使用下標(biāo)訪問(wèn)不存在的元素將導(dǎo)致在map容器中添加一個(gè)新的元素,新元素的key即要查找的內(nèi)容。
復(fù)制代碼 代碼如下:
/*****************************************************************************
* Open file
*****************************************************************************/
ifstream& open_file(ifstream &in, const string &file)
{
in.close(); // close in case it was already open
in.clear(); // clear any existing errors
// if the open fails, the stream will be in an invalid state
in.open(file.c_str()); // open the file we were given
return in; // condition state is good if open succeeded
}
/*****************************************************************************
* Word Transform
*****************************************************************************/
void WordTransform(const string rule, const string infile)
{
if (rule.empty() || infile.empty())
{
return;
}
map<string ,string> trans_map;
string key, value;
// Open transformation file and check that open succeeded
ifstream map_file;
if (!open_file(map_file, rule))
{
throw runtime_error("No transformation file.");
}
// Read the transformation map and build the map
while (map_file >> key >> value)
{
trans_map.insert(make_pair(key, value));
}
// Open the input file and check that the open succeeded
ifstream input;
if (!open_file(input, infile))
{
throw runtime_error("No input file.");
}
string line; // Hold each line from the input
// Read the text to transform it a line at a time
while (getline(input, line))
{
istringstream stream(line); // Read the line a word at a time
string word;
bool bFirstWordFlg = true; // Controls whether a space is printed
while (stream >> word)
{
// ok: the actual mapwork, this part is the heart of the program
map<string, string>::const_iterator map_it = trans_map.find(word);
// If this word is in the transformation map
if (map_it != trans_map.end())
{
// Replace it by the transformaion value in the map
word = map_it->second;
}
if (bFirstWordFlg)
{
bFirstWordFlg = false;
}
else
{
cout << " "; // Print space between words
}
cout << word;
}
cout << endl; // Done with this line of input
}
}
相關(guān)文章
C++中string與int的相互轉(zhuǎn)換實(shí)現(xiàn)代碼
這篇文章主要介紹了C++中string與int的相互轉(zhuǎn)換實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-05-05
C++中putchar與getchar函數(shù)的細(xì)節(jié)及運(yùn)用
C語(yǔ)言提供putchar函數(shù),用于給終端輸出一個(gè)字符;getchar函數(shù),可以從終端接收用戶輸入的一個(gè)字符,本文給大家分享C++中putchar與getchar函數(shù)的細(xì)節(jié)及運(yùn)用,感興趣的朋友跟隨小編一起看看吧2021-07-07
C++?使用?new?創(chuàng)建二維數(shù)組實(shí)例
這篇文章主要介紹了C++?使用?new?創(chuàng)建二維數(shù)組實(shí)例的相關(guān)資料,需要的朋友可以參考下2023-01-01
vscode遠(yuǎn)程連接服務(wù)器(免密登錄+遠(yuǎn)程開(kāi)發(fā))
vscode的遠(yuǎn)程連接功能十分方便,本文就來(lái)介紹一下vscode遠(yuǎn)程連接服務(wù)器,主要包括免密登錄和遠(yuǎn)程開(kāi)發(fā),感興趣的可以了解一下2024-07-07
C++中new/delete與malloc/free的區(qū)別小結(jié)
本文主要介紹了C++中new/delete與malloc/free的區(qū)別小結(jié), malloc、free是C中的庫(kù)函數(shù) new、delete 是C++當(dāng)中的操作符,讀者可以更好地理解C++中內(nèi)存管理的方式和優(yōu)勢(shì)2023-08-08

