C++ 在字符串中查找项并说明何时找到它-c++;

C++ 在字符串中查找项并说明何时找到它-c++;,c++,string,items,C++,String,Items,我有一个项目字符串(参见代码)。我想说的是,什么时候找到了列表中的特定项目。在我的示例中,我希望输出为3,因为该项位于前两项之后。我可以将单独的项目打印到控制台,但我不知道如何对这两个项目进行计数。我想这是因为while循环。。。我总是得到像11这样的数字,而不是两个独立的1。有什么建议吗?:) #包括 #包括 使用名称空间std; int main(){ string items=“盒子、猫、狗、猫”; 字符串delim=“,”; 大小\u t pos=0; 字符串标记; string ite

我有一个项目字符串(参见代码)。我想说的是,什么时候找到了列表中的特定项目。在我的示例中,我希望输出为3,因为该项位于前两项之后。我可以将单独的项目打印到控制台,但我不知道如何对这两个项目进行计数。我想这是因为while循环。。。我总是得到像11这样的数字,而不是两个独立的1。有什么建议吗?:)

#包括
#包括
使用名称空间std;
int main(){
string items=“盒子、猫、狗、猫”;
字符串delim=“,”;
大小\u t pos=0;
字符串标记;
string item1=“dog”;
整数计数=0;
`;
while((pos=items.find(delim))!=string::npos)
{
令牌=项。子项(0,位置);
如果(令牌!=item1)
{

cout我将搜索算法替换为方法
explode
,该方法用分隔符分隔字符串并返回向量,更适合搜索和获取元素计数:

#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;
}
#包括
#包括
#包括
#包括

#include

去罗马有很多方法。这里有一个使用
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;
}
#包括
#包括
#包括
#包括
#包括
常量std::正则表达式re{,“};
int main(){
字符串项{“box,cat,dog,cat”};
//拆分字符串并将所有子项放入向量中
std::vector子项(std::sregx_token_迭代器(items.begin(),items.end(),re,-1),{});
//搜索并检查是否找到并显示结果
if(auto it=std::find(subItems.begin(),subItems.end(),“dog”);it!=subItems.end()

std::可能不相关,但是如果你有
if(condition){…}else if(not_condition){…}
,那就完全像
if(condition){…}else{…}
。如果不需要第二个
,就不需要第二个
。至于你的问题,除非
“dog”
是找到的第一个子字符串,否则在
“dog”之前总会有一个“item”
。所以当你找到
“dog”
时,你可以将其简化为递增的
计数。但是如果“dog”在字符串中出现两次呢?比如:树、狗、房子、猫、狗……我只想说我在第1项之后找到了dog。(这就是为什么我使用else条件来中断while循环的原因,如果是这样的话。为
“dog”
是第一项时添加一个特殊的情况。也可以为您有多个连续的
“dog”时添加一个情况
items。有趣的代码!谢谢。一些我以前没有使用过的新函数。为什么我需要实用程序头文件?我接下来将尝试了解不同的步骤……哦,你是对的,实用程序不是必需的,第二个字符串包括:D。我将编辑我的回复
#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;
}