C++ C++;:查找字符串中最后出现的字符
我已经为此工作了将近两个小时。我相信我的解决方案在逻辑上是正确的,但是我没有得到期望的输出。例如,假设我想查找字符“h”最后一次出现在这个字符串中的时间:“hlhhlh”(如果从0开始,则为6) 我的程序可以编译,但不能运行。此代码仅在char元素中第一次出现“h”时才发现C++ C++;:查找字符串中最后出现的字符,c++,C++,我已经为此工作了将近两个小时。我相信我的解决方案在逻辑上是正确的,但是我没有得到期望的输出。例如,假设我想查找字符“h”最后一次出现在这个字符串中的时间:“hlhhlh”(如果从0开始,则为6) 我的程序可以编译,但不能运行。此代码仅在char元素中第一次出现“h”时才发现 #include <iostream> #include <vector> #include <sstream> using namespace std; int getIndex(ve
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int getIndex(vector<char> arr, char t);
int main()
{
vector<char> myArr(0);
myArr.push_back('l');
myArr.push_back('l');
myArr.push_back('l');
myArr.push_back('hlhh');
int i = getIndex(myArr, 'h');
cout << i << endl;
}
int getIndex(vector<char> myArr, char t)
{
int n=0, m=0, count=0;
string y,s;
vector<string> arr(0);
for(int i =0; i < myArr.size(); i++)
{
stringstream st;
st << myArr[i];
st >> y;
arr.push_back(y);
}
stringstream ss;
ss << t;
ss >> s;
for(int i=0; i < arr.size(); i++)
{
if(arr[i] == "h")
{
n++;
}
else if(arr[i] == "l")
{
m++;
}
}
for(int i=0; i< arr.size(); i++)
{
if(arr[i]==s)
{
count++;
if(count == n)
{
return i;
}
else if(count == m)
{
return i;
}
}
}
#包括
#包括
#包括
使用名称空间std;
int getIndex(向量arr,字符t);
int main()
{
载体myArr(0);
myArr.推回(“l”);
myArr.推回(“l”);
myArr.推回(“l”);
myArr.push_back('hlhh');
int i=getIndex(myArr,'h');
库特;
对于(int i=0;i
}您需要
std::string::rfind
:
std::string s = "hlhhhlh";
std::cout << "The last 'h' in '" << s << "' is at position "
<< s.rfind('h') << std::endl; // output here is: The last 'h' in hlhhhlh is at position 6
std::string s=“hlhhhlh”;
std::cout您想要std::string::rfind
:
std::string s = "hlhhhlh";
std::cout << "The last 'h' in '" << s << "' is at position "
<< s.rfind('h') << std::endl; // output here is: The last 'h' in hlhhhlh is at position 6
std::string s=“hlhhhlh”;
std::cout您正在添加以下内容:“hlhh”是一个多字节字符
myArr.push_back('hlhh');
它们需要单独添加,即
myArr.push_back('h');
myArr.push_back('l');
myArr.push_back('h');
myArr.push_back('h');
您正在添加以下内容:“hlhh”是一个多字节字符
myArr.push_back('hlhh');
它们需要单独添加,即
myArr.push_back('h');
myArr.push_back('l');
myArr.push_back('h');
myArr.push_back('h');
<代码> 'HLHH '/COD>不是C++字符串,字符向量只能是< > PoSpReBuff< /Cord>单字符:
myArr.push_back('l');
myArr.push_back('l');
myArr.push_back('l');
myArr.push_back('h');
myArr.push_back('l');
myArr.push_back('h');
myArr.push_back('h');
更好的做法是,getIndex
函数应该是:
int getIndex(vector<char> &myArr, char t);
intgetindex(vector&myArr,chart);
而不是
int getIndex(vector<char> myArr, char t);
intgetindex(向量myArr,chart);
<> >由于<代码>值> <代码>将生成新的<代码>向量对象,并复制输入对象的所有元素,从而产生性能开销。 < > >代码> HLHH '/Cuth>不是C++字符串,字符向量只能是<代码> PurpSuff< <代码>单字符:
myArr.push_back('l');
myArr.push_back('l');
myArr.push_back('l');
myArr.push_back('h');
myArr.push_back('l');
myArr.push_back('h');
myArr.push_back('h');
更好的做法是,getIndex
函数应该是:
int getIndex(vector<char> &myArr, char t);
intgetindex(vector&myArr,chart);
而不是
int getIndex(vector<char> myArr, char t);
intgetindex(向量myArr,chart);
因为通过值传递将生成新的向量对象,并复制输入对象的所有元素,从而产生性能开销。您可以使用字符串和rfind()来查找字符串中最后出现的字符,如“Kerrek SB”所建议的。
如果您想将字符串与向量一起使用,那么下面的示例代码将帮助您
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector<string> arr(0);
arr.push_back(string("h"));
arr.push_back(string("l"));
arr.push_back(string("h"));
arr.push_back(string("l"));
arr.push_back(string("h"));
arr.push_back(string("h"));
arr.push_back(string("l"));
arr.push_back(string("h"));
arr.push_back(string("l"));
arr.push_back(string("h"));
arr.push_back(string("l"));
for (int i=arr.size()-1; i >=0; i--)
if (arr[i] == string("h")) {
cout<<"\n H is present at"<< i;
break;
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
向量arr(0);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“l”);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“l”);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“l”);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“l”);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“l”);
对于(int i=arr.size()-1;i>=0;i--)
if(arr[i]==字符串(“h”)){
cout您可以使用String with rfind()查找字符串中最后出现的字符,如“Kerrek SB”所示。
如果您想将字符串与向量一起使用,那么下面的示例代码将帮助您
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector<string> arr(0);
arr.push_back(string("h"));
arr.push_back(string("l"));
arr.push_back(string("h"));
arr.push_back(string("l"));
arr.push_back(string("h"));
arr.push_back(string("h"));
arr.push_back(string("l"));
arr.push_back(string("h"));
arr.push_back(string("l"));
arr.push_back(string("h"));
arr.push_back(string("l"));
for (int i=arr.size()-1; i >=0; i--)
if (arr[i] == string("h")) {
cout<<"\n H is present at"<< i;
break;
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
向量arr(0);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“l”);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“l”);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“l”);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“l”);
arr.push_back(字符串(“h”);
arr.push_back(字符串(“l”);
对于(int i=arr.size()-1;i>=0;i--)
if(arr[i]==字符串(“h”)){
“差不多几个小时”行吗?这是什么意思?不算几个小时,所以只要50分钟?使用std::string
并调用find_last_of()
会更好?你为什么不调试你的程序?这真的很有帮助。“差不多几个小时”这意味着什么?不太多,所以只要50分钟?使用std::string
并调用find_last_of()
会更好?为什么不调试程序?这真的很有帮助。