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



我正在尝试从该URL中获取所有选项(oqaqs等(。我正在使用一个函数来单独提取每一个。然而,我不知道如何在循环到达最终选项后终止它。我想我正在将res设置为当前位置,但我不确定。我尝试在!(res == url.length())时终止它,但没有成功。有人能帮忙吗?

此外,我希望您能就如何将其扩展到多个URLS提供建议。我假设我必须使用另一个循环,但我仍在学习。

#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对。

如果找不到下一个键,我建议更改函数以返回bool。然后,当函数返回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;
}

输出:

q=哪里+食物+附近+我oq=哪里+食物+附近+我aqs=铬。。69i57j0l5.8995j0j9sourceid=chromeie=UTF-8

实时演示

最新更新