在字符串中查找项目并说出何时找到 - C++
Finding item in string and say WHEN it was found - c++
我有一串项目(见代码)。我想说的是何时找到该列表中的特定项目。在我的示例中,我希望输出为 3,因为该项目是在前两项之后找到的。我可以将单独的项目打印到控制台,但我不知道如何对这两项进行计数。我认为这是因为 while 循环......我总是得到像 11 这样的数字而不是两个单独的 1。有小费吗? :)
#include <iostream>
#include <string>
using namespace std;
int main() {
string items = "box,cat,dog,cat";
string delim = ",";
size_t pos = 0;
string token;
string item1 = "dog";
int count = 0;
`;
while ((pos = items.find(delim)) != string::npos)
{
token = items.substr(0, pos);
if (token != item1)
{
cout << token << endl; //here I would like to increment count for every
//item before item1 (dog) is found
items.erase(0, pos + 1);
}
else if (token == item1)
return 0;
}
return 0; //output: box cat
}
我用 explode
方法替换了您的搜索算法,该方法用定界符和 returns 向量分隔字符串,它更适合搜索和获取元素计数:
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
std::vector<std::string> explode(const std::string& s, char delim)
{
std::vector<std::string> result;
std::istringstream iss(s);
for (std::string token; std::getline(iss, token, delim); )
{
result.push_back(std::move(token));
}
return result;
}
int main()
{
std::string items = "box,cat,dog,cat";
std::string item1 = "dog";
char delim = ',';
auto resultVec = explode(items, delim);
auto itResult = std::find_if(resultVec.begin(), resultVec.end()
, [&item1](const auto& resultString)
{
return item1 == resultString;
});
if (itResult != resultVec.end())
{
auto index(std::distance(resultVec.begin(), itResult) + 1); // index is zero based
std::cout << index;
}
return 0;
}
通过使用std::find_if
,您可以通过迭代器获取item1
的位置,您可以将其与std::distance
一起使用来获取它前面的元素的数量。
explode
方法的学分转至此 post:Is there an equivalent in C++ of PHP's explode() function?
去罗马有很多方法。这是使用 std::regex
.
的附加解决方案
但主要方法与接受的答案相同。使用现代C++17语言元素,更简洁一点。
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
#include <vector>
const std::regex re{ "," };
int main() {
std::string items{ "box,cat,dog,cat" };
// Split String and put all sub-items in a vector
std::vector subItems(std::sregex_token_iterator(items.begin(), items.end(), re, -1), {});
// Search and check if found and show result
if (auto it = std::find(subItems.begin(), subItems.end(), "dog"); it != subItems.end())
std::cout << "Found at position: " << std::distance(subItems.begin(), it) + 1 << '\n';
else
std::cout << "Not found.\n";
return 0;
}
我有一串项目(见代码)。我想说的是何时找到该列表中的特定项目。在我的示例中,我希望输出为 3,因为该项目是在前两项之后找到的。我可以将单独的项目打印到控制台,但我不知道如何对这两项进行计数。我认为这是因为 while 循环......我总是得到像 11 这样的数字而不是两个单独的 1。有小费吗? :)
#include <iostream>
#include <string>
using namespace std;
int main() {
string items = "box,cat,dog,cat";
string delim = ",";
size_t pos = 0;
string token;
string item1 = "dog";
int count = 0;
`;
while ((pos = items.find(delim)) != string::npos)
{
token = items.substr(0, pos);
if (token != item1)
{
cout << token << endl; //here I would like to increment count for every
//item before item1 (dog) is found
items.erase(0, pos + 1);
}
else if (token == item1)
return 0;
}
return 0; //output: box cat
}
我用 explode
方法替换了您的搜索算法,该方法用定界符和 returns 向量分隔字符串,它更适合搜索和获取元素计数:
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
std::vector<std::string> explode(const std::string& s, char delim)
{
std::vector<std::string> result;
std::istringstream iss(s);
for (std::string token; std::getline(iss, token, delim); )
{
result.push_back(std::move(token));
}
return result;
}
int main()
{
std::string items = "box,cat,dog,cat";
std::string item1 = "dog";
char delim = ',';
auto resultVec = explode(items, delim);
auto itResult = std::find_if(resultVec.begin(), resultVec.end()
, [&item1](const auto& resultString)
{
return item1 == resultString;
});
if (itResult != resultVec.end())
{
auto index(std::distance(resultVec.begin(), itResult) + 1); // index is zero based
std::cout << index;
}
return 0;
}
通过使用std::find_if
,您可以通过迭代器获取item1
的位置,您可以将其与std::distance
一起使用来获取它前面的元素的数量。
explode
方法的学分转至此 post:Is there an equivalent in C++ of PHP's explode() function?
去罗马有很多方法。这是使用 std::regex
.
但主要方法与接受的答案相同。使用现代C++17语言元素,更简洁一点。
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
#include <vector>
const std::regex re{ "," };
int main() {
std::string items{ "box,cat,dog,cat" };
// Split String and put all sub-items in a vector
std::vector subItems(std::sregex_token_iterator(items.begin(), items.end(), re, -1), {});
// Search and check if found and show result
if (auto it = std::find(subItems.begin(), subItems.end(), "dog"); it != subItems.end())
std::cout << "Found at position: " << std::distance(subItems.begin(), it) + 1 << '\n';
else
std::cout << "Not found.\n";
return 0;
}