2014-01-23 87 views
0

我是C++新手,C++函数的功能如下。 查找文件:C++字符串匹配和替换使用查找文件

POST OFFICE,PO 
SUITE ACCESS ROOM, SAR 
SUITE,STE 
STREET,ST 
NEW YORK,NY 
POST,PST 
LONG LINE STREET,LLS 

会有哪些应该有像“ARIJIT, 192 POST OFFICE, SUITE”一个参数,它会给像“ARIJIT, 192 PO, STE”输出一个C++函数。它将使用一个静态查找文件,如上层内容。

我做了下面的代码结构,但没有找到在哪个方向去..

#include <iostream> 
#include <fstream> 
#include <string> 
int main() 
{ 
    char * processAddress (char *input); 
    char *input = "ARIJIT, 192 POST OFFICE, SUITE"; 
    char *output = processAddress(input); 
    std::cout << output; 
    return 0; 
} 

char * processAddress (char *input) { 
    char *output = input; 

    std::ifstream file("LookUp.csv"); 
    std::string str; 
    while (std::getline(file, str)) 
    { 
     std:: cout << str << '\n'; 
    } 
    return output; 
} 

问题,我面对

1. How to map look up file 

2. Find and Replace 

在此先感谢。

+0

您必须阅读文件并从中创建数据结构(地图)。然后,您必须尝试在该参数中找到所有关键字符串(“POST OFFICE”),然后*然后*您将能够使用替换。但要注意一个关键字符串是另一个关键字符串的可能性,如在“STREET”和“LONG LINE STREET”中。按降序排列键字符串可能会有所帮助。 – laune

+0

您的第一行没有显示您为解决此问题所做的个人努力。你需要给我们一个例子。你的意见是什么,你的预期和实际产出是多少?你是如何读取文件并存储它的内容的? – poljpocket

回答

1

谢谢伙计

我在下面解决它的代码。

#include <iostream> 
#include <fstream> 
#include <string> 
#include <map> 
int main() 
{ 
    std::string processAddress (std:: string input); 
    std::string input = "ARIJIT, 192 POST OFFICE, SUITE"; 
    std::string output = processAddress(input); 
    std::cout << output << "\n"; 
    return 0; 
} 

std::string processAddress (std:: string input) { 
    void replaceAll(std::string& str, const std::string& from, const std::string& to); 
    std::ifstream file("LookUp.csv"); 
    std::string str; 
    typedef std::map<std::string, std::string> MyMap; 
    MyMap my_map; 
    while (std::getline(file, str)) 
    { 
     std::string delimiter = ","; 
     std::string token1 = str.substr(0, str.find(delimiter)); 
     std::string token2 = str.substr(token1.length()+1, str.find(delimiter)); 
     my_map[token1] = token2; 
    } 
    // Map Enumeration 
    for(MyMap::const_iterator it = my_map.end(); it != my_map.begin(); --it) 
    { 
     std::string key = it->first; 
     std::string value = it->second; 
     // find and replace 
     replaceAll(input, key, value); 
    } 

    std::string output = input ; 
    return output; 
} 
bool replace(std::string& str, const std::string& from, const std::string& to) { 
    size_t start_pos = str.find(from); 
    if(start_pos == std::string::npos) 
     return false; 
    str.replace(start_pos, from.length(), to); 
    return true; 
} 
void replaceAll(std::string& str, const std::string& from, const std::string& to) { 
    if(from.empty()) 
     return; 
    size_t start_pos = 0; 
    while((start_pos = str.find(from, start_pos)) != std::string::npos) { 
     str.replace(start_pos, from.length(), to); 
     start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' 
    } 
} 
0

假设你已经加载该文件中的字符串,分裂的逗号,并将其存储在一个可访问的方式,取代叫你正在寻找的应该是这样的:

size_t pos = input.find(toReplace); // toReplace = "text to replace" 
if (pos != std::string::npos) { 
    input.replace(pos, toReplace.length(), shorterString); // shorterString = "new text" 
} else { 
    // Your "text to replace" doesn't exist in input 
}