我正在尝试编写在 char*
数组中搜索 char *
元素的函数,如果该元素存在于数组中,该函数将开始检查该元素我将“找到”,如果没有,则应将其“插入”并将元素添加到数组中。
我写了这段代码,但我不知道如何尝试,程序总是给我异常,我该怎么做才能检查指针数组中的元素?
void checkFunction(char*myArray[], char *element,bool flag)
{
for (int i = 0; i < strlen(*myArray) ; ++i)
{
if (myArray[i] == element)
{
flag = true;
}
}
*myArray = element;
flag = false;
if (flag)
{
cout << "Found" << endl;
}
else
{
cout << "Inserted" << endl;
}
}
最佳答案
C++ 方式
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, const char * argv[]) {
vector<string> myStrings { "One", "Two", "Three" };
// std::find() finds the first element that matches a value
auto it = find(begin(myStrings), end(myStrings), "Twooo");
if (it != end(myStrings)) {
cout << "We found this string; do something..." << endl;
}
}
关于c++ - 在 C++ 中查找 char* 数组中的 char* 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34387181/