C++ 如何在考虑标点符号的同时查找文件中的字符串?

C++ 如何在考虑标点符号的同时查找文件中的字符串?,c++,C++,我试图计算某个单词在文本文件中出现的次数。这是我的密码: int main() { ifstream file("Montreal.txt"); string str; int number = 0; while (file >> str){ if (str == "Toronto"){ number++; } } cout << number << end

我试图计算某个单词在文本文件中出现的次数。这是我的密码:

int main()
{
    ifstream file("Montreal.txt");
    string str;
    int number = 0;
    while (file >> str){
        if (str == "Toronto"){
            number++;
        }
    }
    cout << number << endl;
    return 0;
}
intmain()
{
ifstream文件(“Montreal.txt”);
字符串str;
整数=0;
while(文件>>str){
如果(str==“多伦多”){
数字++;
}
}

不能使用
std::string::find()


试试这样的

        #include <iostream>
        #include <fstream>
        #include <string>
        #include <vector>
        #include <limits>
        #include <sys/stat.h>

        const size_t nErrSize = std::numeric_limits<size_t>::max();

        int CountWord(const std::string &sBuff, const std::string sWord)
        {
            size_t nNext = 0;
            int nCount = 0;
            int nLength = sWord.length();

            while (sBuff.npos != (nNext = sBuff.find(sWord, nNext)))
            {
                   ++nCount;
                   nNext += nLength;
            }
            return nCount;
        }

        #if defined(WIN32)
            #undef stat
            #define stat _stat
        #endif

        size_t GetFileSize(const std::string &sFile)
        {
            struct stat st = { 0 };
            if (0 > ::stat(sFile.c_str(), &st))
                return nErrSize;
            else
                return st.st_size;
        }

        bool Load(const std::string &sFile, std::string &sBuff)
        {
            size_t nSize = GetFileSize(sFile);
            if (nSize == nErrSize)
                return false;

            std::ifstream ifs(sFile, std::ifstream::binary);
            if (!ifs)
                return false;

            std::vector<char> vBuff(nSize);
            ifs.read(vBuff.data(), nSize);
            if (ifs.gcount() != nSize)
                return false;

            sBuff.assign(vBuff.cbegin(), vBuff.cend());

            return true;
        }

        int main()
        {
            const std::string sFile("Montreal.txt");
            const std::string sSearch("Toronto");
            std::string sBuff;
            if (Load(sFile, sBuff))
            {
                std::cout << sSearch
                          << " occurred "
                          << CountWord(sBuff, sSearch)
                          << " times in file "
                          << sFile
                          << "."
                          << std::endl;
                return 0;
            }
            else
            {
                std::cerr << "Error" << std::endl;
                return 1;
            }
        }
#包括
#包括
#包括
#包括
#包括
#包括
const size_t nerrize=std::numeric_limits::max();
int CountWord(const std::string&sBuff,const std::string剑)
{
大小n下一步=0;
int nCount=0;
int nLength=剑的长度();
而(sBuff.npos!=(nNext=sBuff.find(剑,nNext)))
{
++n计数;
nNext+=nLength;
}
返回n计数;
}
#如果已定义(WIN32)
#未定义状态
#定义stat\u stat
#恩迪夫
大小\u t GetFileSize(常量std::string和sFile)
{
struct stat st={0};
if(0>::stat(sFile.c_str(),&st))
返回能化;
其他的
返回st.st_大小;
}
bool加载(常量std::string和sFile,std::string和sBuff)
{
size\u t nSize=GetFileSize(sFile);
如果(nSize==nErrSize)
返回false;
std::ifstream ifs(sFile,std::ifstream::binary);
如果(!ifs)
返回false;
标准:向量vBuff(nSize);
ifs.read(vBuff.data(),nSize);
如果(ifs.gcount()!=nSize)
返回false;
赋值(vBuff.cbegin(),vBuff.cend());
返回true;
}
int main()
{
const std::string sFile(“Montreal.txt”);
const std::字符串搜索(“多伦多”);
std::字符串sBuff;
if(加载(sFile、sBuff))
{
std::cout@mzee99字符串“Torontour”怎么样?您也需要计算这样的单词吗?
        #include <iostream>
        #include <fstream>
        #include <string>
        #include <vector>
        #include <limits>
        #include <sys/stat.h>

        const size_t nErrSize = std::numeric_limits<size_t>::max();

        int CountWord(const std::string &sBuff, const std::string sWord)
        {
            size_t nNext = 0;
            int nCount = 0;
            int nLength = sWord.length();

            while (sBuff.npos != (nNext = sBuff.find(sWord, nNext)))
            {
                   ++nCount;
                   nNext += nLength;
            }
            return nCount;
        }

        #if defined(WIN32)
            #undef stat
            #define stat _stat
        #endif

        size_t GetFileSize(const std::string &sFile)
        {
            struct stat st = { 0 };
            if (0 > ::stat(sFile.c_str(), &st))
                return nErrSize;
            else
                return st.st_size;
        }

        bool Load(const std::string &sFile, std::string &sBuff)
        {
            size_t nSize = GetFileSize(sFile);
            if (nSize == nErrSize)
                return false;

            std::ifstream ifs(sFile, std::ifstream::binary);
            if (!ifs)
                return false;

            std::vector<char> vBuff(nSize);
            ifs.read(vBuff.data(), nSize);
            if (ifs.gcount() != nSize)
                return false;

            sBuff.assign(vBuff.cbegin(), vBuff.cend());

            return true;
        }

        int main()
        {
            const std::string sFile("Montreal.txt");
            const std::string sSearch("Toronto");
            std::string sBuff;
            if (Load(sFile, sBuff))
            {
                std::cout << sSearch
                          << " occurred "
                          << CountWord(sBuff, sSearch)
                          << " times in file "
                          << sFile
                          << "."
                          << std::endl;
                return 0;
            }
            else
            {
                std::cerr << "Error" << std::endl;
                return 1;
            }
        }