繁体   English   中英

如何在字符串末尾终止循环?

[英]How to terminate loop at end of string?

我正在尝试从此 URL ( oqaqs等)中获取所有选项。 我正在使用 function 单独提取每个。 但是,一旦达到最终选项,我无法弄清楚如何终止循环。 我想我正在将res设置为当前的 position,但我不确定。 我尝试在.(res == url.length())时终止它,但这不起作用。 有人能帮忙吗?

另外,我希望得到有关如何将其扩展到多个 URL 的建议。 我假设我必须使用另一个循环,但我仍在学习它们。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string getKey(const string keyval, size_t start, size_t& second);

using namespace std;

int main()
{
    int count = 0;
    size_t res;
    string url = "https://www.google.com/search?q=where+food+near+me&oq=where+food+near+me&aqs=chrome..69i57j0l5.8995j0j9&sourceid=chrome&ie=UTF-8";
    while(count < 20){
        cout << getKey(url,0, res) << endl;
        while (!(res == url.length()){
        cout << getKey(url,res, res) << endl;
            ++count;
        }
        cout<< res << endl;
        ++count;
   }

    return 0;
}

string getKey(const string keyval, size_t start, size_t& second)
{
    size_t first;
    string result;
    if(start == 0){
        first = keyval.find('&');
    }
    else{
        first = start;
    }
    first = first + 1;
    second = keyval.find('&' , first);
    result = keyval.substr(first, second - first);
    return result;
}

更新

#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

namespace helpers {
size_t getDomainName(const string str, string& result);
size_t getKeyVal(const string str, const size_t start_pos, string& result);
bool getKey(const string &keyval, size_t start, size_t& second, string &result);
string getVal(const string keyval);
}
using namespace helpers;


int main() {
    string fileName;
    int count = 0;
    int total_query = 0, total_length = 0, avg_length = 0;
    string URL,str;
    int total_Urls = 0;
    string line;
    string res,key,val;
    cout << "Please input the name of the file: " << endl;
    cin >> fileName;
    ifstream fin;
    fin.open (fileName);
    if (fin.is_open()){
        cout << "File opened successfully...\n";
    }
    else {
        cout << "File failed to open...\n";
        return 0;
    }

    // Finding total URL count...
    for (string URL; fin >> URL;){
       ++total_Urls;
    }
    //cout << total_Urls << endl;
    fin.close();
    fin.open(fileName);

    count = 0;

    //average length of URL
    while (count < total_Urls){
        fin >> str;
       // cout << "The length of the URL: "<<str.length() <<endl;
    total_length += str.length();
        ++count;
    }
    avg_length = total_length / total_Urls;
   // cout << "Average length: "<< avg_length << endl;

    fin.close();
    fin.open(fileName);



    count = 0;

    //average length of query
    while(count < total_Urls){
        fin >> URL;
        getKeyVal(URL, 0, res);
        res = res.substr(2);
        replace(res.begin(), res.end(), '+', ' ');
       // cout << "Test: " << res << endl;
       // cout << "Length of Query: " << res.length() << endl;
        total_query += res.length();
        ++count;

    }
    int avg_query_length = total_query / count;
    //cout << "Average length of query: " << avg_query_length << endl;

    fin.close();
    fin.open(fileName);

    //dealing with options

   /* while(count < 20){
         cout << getKey(url,0, res) << endl;
         while (count < 5){
         cout << getKey(url,res, res) << endl;
             ++count;
         }
         cout<< res << endl;
         ++count;
    } */

    count = 0;
    string value;
    size_t res2 = 0;
    while (count < total_Urls){
        fin >> URL;

    while ((count < 20) && getKey(URL, res2, res2, value)){
        cout << value << endl;
        ++count;
    }
    }

    cout << "Statistics for <" <<fileName << ">" << endl;
    cout << "--------------------------" << endl;
    cout << "Number of Queries: " << total_Urls << endl;
    cout << "Average length of URL: " << avg_length << endl;
//    cout << "Average length of Query String: " << avg_query;

    return 0;
}

namespace helpers {

size_t getDomainName (const string str, string& result){
size_t first, second;
first = str.find('/');
first = str.find('/', first + 1);
second = str.find('/', first + 1);
result = str.substr(first + 1, second - (first + 1));
return second;
}
size_t getKeyVal(const string str, const size_t start_pos, string& result){
    size_t first, second;
    if(start_pos == 0) {
        first = str.find('?');
    }
    else {
        first = start_pos;
    }
    second = str.find('&', first + 1);
    result = str.substr(first + 1, second - (first + 1));
    return second;
}
bool getKey(const string &keyval, size_t start, size_t& second, string &result)
{
    result = "";

    if (start == 0) {
        if ((start = keyval.find('?')) == string::npos)
            return false;
        ++start;
    }
    else if (start >= keyval.size())
        return false;

    size_t end = keyval.find('&', start);
    if (end != string::npos) {
        result = keyval.substr(start, end - start);
        second = end + 1;
    }
    else {
        result = keyval.substr(start);
        second = keyval.size();
    }
    return true;
}

}
string getVal(const string keyval){
    size_t first;
    string result;
    first = keyval.find('=');
    first = first + 1;
    result = keyval.substr(first);
    replace(result.begin(), result.end(), '+', ' ');
    return result;
}


您没有考虑到当找到最后一个&时,对find('&')的后续调用将返回std::string::npos ,即-1,而不是您期望的字符串长度。 因此,您无法正确返回最终的key=value对。

如果找不到下一个键,我建议更改 function 以返回bool 然后您可以在 function 返回 false 时中断循环。 退出时, res应设置为找到&之后下一个标记的位置,而不是&本身。

尝试更多类似的东西:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool getKey(const string &keyval, size_t start, size_t& second, string &value);

int main()
{
    int count = 0;
    string url = "https://www.google.com/search?q=where+food+near+me&oq=where+food+near+me&aqs=chrome..69i57j0l5.8995j0j9&sourceid=chrome&ie=UTF-8";
    string value;
    size_t res = 0;
    while ((count < 20) && getKey(url, res, res, value)) {
        cout << value << endl;
        ++count;
    }

    return 0;
}

bool getKey(const string &keyval, size_t start, size_t& second, string &result)
{
    result = "";

    if (start == 0) {
        if ((start = keyval.find('?')) == string::npos)
            return false;
        ++start;
    }
    else if (start >= keyval.size())
        return false;

    size_t end = keyval.find('&', start);
    if (end != string::npos) {
        result = keyval.substr(start, end - start);
        second = end + 1;
    }
    else {
        result = keyval.substr(start);
        second = keyval.size();
    }
    return true;
}

Output:

q=where+food+near+me
oq=where+food+near+me
aqs=chrome..69i57j0l5.8995j0j9
sourceid=chrome
ie=UTF-8

现场演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM