在字符串 C++ 中查找索引
Finding Index in string C++
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int contain(vector<string> &s_vector, string check)
{
cout<<"Input a string to check: ";
cin>>check;
if(s_vector=check)
return find(&s_vector);
else
return -1;
}
int main(){
vector<string>s_vector;
cout<<"Input the size of string vector: ";
int size;
cin>>size;
cout<<"Input "<<size<<"strings: n";
cin>>s_vector[size];
cout<<"Strings in the string vector: ";
for(int i=0; i<s_vector.size(); i++)
cout<<s_vector[i]<<" ";
contain(s_vector, string check);
return 0;
}
我正在尝试制作一个代码,您可以在其中找到字符串的索引。例如,输出如下所示:
Input the size of string vector: 3
Input 3 strings:
asd
lkj
qere
Input a string to check: lkj
1
但是int contain~section中似乎有一些错误,如果我取出int contain~部分并运行程序,每当我尝试输入字符串时,它都会一直说"运行已停止"。我是C++新手,所以我真的可以使用您的帮助,谢谢。
包含函数可能是什么样子的:
int contain(const vector<string>& s_vector)
{
cout << "Input a string to check: ";
string check;
cin >> check;
for (int i = 0; i < s_vector.size(); i++)
if (s_vector[i] == check)
return i;
return -1;
}
无需将局部变量check
传递给函数。我们使用运算符 [] 逐个比较向量内的字符串。主要看起来像这样:
int main()
{
vector<string>s_vector;
cout<<"Input the size of string vector: ";
int size;
cin>>size;
cout << "Input " << size << "strings: n";
for (int i = 0; i < size; i++)
{
string str;
cin >> str;
s_vector.push_back(str);
}
cout<<"Strings in the string vector: ";
for(int i=0; i<s_vector.size(); i++)
cout<<s_vector[i]<<" ";
int i = contain(s_vector);
cout << "Index: " << i;
return 0;
}
相关文章:
- 查找最接近的大于当前数字的数字的索引
- 查找字符在两个索引之间出现的次数
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 在对向量中查找元素的索引
- 查找索引的最接近的真实值
- C++ char 类查找字符索引
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 查找数组中重复(重复)数字的索引
- 查找下一个具有真值C++的数组索引
- 在不进行排序的情况下查找数组中n个最小值的索引
- 在索引0为0的数组中查找非零最小值C++
- 查找索引数据结构,例如`sTD :: vector`(非数组)
- 查找索引值在char表中
- C++ std::lower_bound() 函数来查找索引排序向量的插入点
- 在数组中查找索引 给出它之前和之后的总和
- 使用向量时查找索引<>::迭代器
- 如何在Poco::DeflatingOutputStream中查找索引
- 如何在首次出现最大值的排序数组中查找索引
- 在字符串 C++ 中查找索引
- 使用AddFontResource()临时添加到系统字体表中的字体的C++查找索引,以便在控制台中使用