返回字符串函数
Return of a string function
我无法return
某些strings
是否出现多次。
即我有两个vectors
,我用2nd
vector
搜索1st
vector
,如果2nd vector
中的某些elements
在1st vector
中出现不止一次,我想return
错误,但由于某种原因,我只能return
1st vector
中的元素是否出现多次
我的代码如下
我想return s1
当元素出现不止一次时,我该怎么做,我尝试将其放在break
前面,但没有奏效
std::vector<std::string> test; //vector that comes in
test.push_back("YES");
test.push_back("YES");
//test.push_back("NO");
test.push_back("NO");
std::vector<std::string> test1; // vector from DB..
test1.push_back("YES");
test1.push_back("NO");
std::string s ("Element count is fine");
std::string s1 ("Element count is incorrect");
for(int i = 0; i < test1.size(); i++)
{
if(count(test.begin(), test.end(),test1[i]) > 1)
{
return s1;
}
}
return s;
将循环更改为:
for(int i = 0; i < test1.size(); i++)
{
if(count(test.begin(), test.end(),test1[i]) > 1)
{
// DCS_LOG_DEBUG("Some elements have appeared more than once...");
return s1;
}
}
return
脱离所有控制结构并离开当前函数。您的旧代码几乎每次都返回s1
,因为return s1
不受if
保护。
我认为代码是正确的。它在我的计算机上工作正常。也许你可以在 if 中输出一些东西,看看到底发生了什么。
if(count(test.begin(), test.end(),test1[i]) > 1)
{
cout<<"The count is "<<count(test.begin(), test.end(),test1[i])<<endl;
return s1;
}
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何从void函数输出字符串
- 使用用户定义函数的字符串反转
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- 在c++构造函数中使用随机字符串生成器
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- C++:如何使函数只返回作为列表一部分的字符串
- 如何在C++中的同一函数中使用字符串和双精度
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 一个函数,用于查找字符串1包含字符串2 c++的次数
- 对函数 PlaySound 使用字符串变量
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- Visual c ++,使用字符串引用/指针调用 dll 函数
- 构造函数字符串参数未设置窗口标题
- C++默认构造函数:字符串参数与字符串参数()
- 使用函数C++字符串输入
- c++模板函数字符串类型不匹配