使用map實現(xiàn)單詞轉(zhuǎn)換的實例分析
更新時間:2013年05月28日 15:38:28 作者:
本篇文章是對使用map實現(xiàn)單詞轉(zhuǎn)換的代碼實例進(jìn)行了纖細(xì)的分析介紹,需要的朋友參考下
使用map實現(xiàn)單詞轉(zhuǎn)換的實例分析
從map中查找單詞時必須使用find函數(shù),不能使用下表,因為在map中使用下標(biāo)訪問不存在的元素將導(dǎo)致在map容器中添加一個新的元素,新元素的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中查找單詞時必須使用find函數(shù),不能使用下表,因為在map中使用下標(biāo)訪問不存在的元素將導(dǎo)致在map容器中添加一個新的元素,新元素的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++指針數(shù)組、數(shù)組指針、數(shù)組名及二維數(shù)組技巧匯總
這篇文章主要介紹了C++指針數(shù)組、數(shù)組指針、數(shù)組名及二維數(shù)組技巧匯總,對于深入理解C++數(shù)組與指針來說非常重要,需要的朋友可以參考下2014-08-08C++ getcwd函數(shù)獲取項目運(yùn)行路徑方法詳解
在Linux下做QT項目時,需要獲取項目的運(yùn)行路徑,于是用getcwd函數(shù)進(jìn)行獲取,然后在Windows下進(jìn)行測試,發(fā)現(xiàn)獲取到的是程序的項目路徑,即代碼文件路徑,然后再Linux QT中測試,獲取到的又是運(yùn)行路徑,這就很納悶了。經(jīng)過再三測試,終于發(fā)現(xiàn)了原因2022-10-10c++雙向鏈表操作示例(創(chuàng)建雙向鏈、雙向鏈表中查找數(shù)據(jù)、插入數(shù)據(jù)等)
這篇文章主要介紹了c++雙向鏈表操作示例,包括創(chuàng)建雙向鏈、刪除雙向鏈表、雙向鏈表中查找數(shù)據(jù)、插入數(shù)據(jù)等,需要的朋友可以參考下2014-05-05linux根據(jù)pid獲取進(jìn)程名和獲取進(jìn)程pid(c語言獲取pid)
status文件,第一行的Name即為進(jìn)程名,C程序?qū)崿F(xiàn)根據(jù)PID獲取進(jìn)程名和根據(jù)進(jìn)程名獲取PID,大家參考使用吧2013-12-12一篇文章徹底弄懂C++虛函數(shù)的實現(xiàn)機(jī)制
C++中的虛函數(shù)的作用主要是實現(xiàn)了多態(tài)的機(jī)制,基類定義虛函數(shù),子類可以重寫該函數(shù),在派生類中對基類定義的虛函數(shù)進(jìn)行重寫時,需要在派生類中聲明該方法為虛方法,這篇文章主要給大家介紹了關(guān)于如何通過一篇文章徹底弄懂C++虛函數(shù)的實現(xiàn)機(jī)制,需要的朋友可以參考下2021-06-06