我如何在C 中解析矢量
How can I parse a vector to a bool in C++?
我正在尝试编写一个程序,该程序首先检查矢量中的名称是否在,如果没有,则将其添加到向量。我的代码似乎很难解析,至少这就是我从中得到的。我尝试将字符串更改为char,但对我没有太大帮助。
#include <iostream>
#include <vector>
#include <string>
bool isinVector(std::string uElement, std::vector<std::string> uArray)
{
for (unsigned int i = 0; i <= sizeof(uArray); i++) {
if (uArray[i] == uElement) {
return true;
}
else {
return false;
}
}
}
int main()
{
bool trigger = false;
while (!trigger) {
std::vector<std::string> names;
names.push_back("Bart");
std::string newName;
getline(std::cin, newName);
if (isinVector(newName, names))
{
std::cout << "true" << std::endl;
trigger = true;
}
else
{
std::cout << "false" << std::endl;
names.push_back(newName);
for (int i = 0; i <= sizeof(names); i++) {
std::cout << names[i] << std::endl;
}
}
}
}
我对您的代码进行了一些调整,删除了您的ISInVector函数,然后在主函数内使用lambda。将来请提供一个简洁的问题和例子。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::find_if;
int main(){
bool trigger = false;
while (!trigger) {
vector<string> names;
names.push_back("Bart");
string newName;
getline(cin, newName);
if(find_if(names.begin(), names.end(), [newName] (const string& name){
return !name.compare(newName);
}) != names.end()){
cout << "true" << endl;
trigger = true;
}
else{
cout << "false" << endl;
names.push_back(newName);
for (size_t i = 0; i < names.size(); i++) {
cout << names.at(i) << endl;
}
}
}
return 0;
}
代码使用std :: find_if检查元素是否存在于向量中。如果std::find_f
没有将迭代器返回到uArray.end()
,则该元素存在。还有您的循环使用的尺寸不正确,请使用vector.size方法。而且您一直在循环直到<=
,它应该是< uArray.size()
,并且通过.AT方法而不是索引[]
访问向量中的元素是更安全的
在更新的帖子中的错误中。
- 不当使用
sizeof
- 重新发明标准算法
- 缺乏错误检查
考虑您要完成的任务。您想:
- 初始化包含名称的起始向量
不断阅读新名称。对于每个新名称,请阅读:
a。检查它是否已经在向量中。
- 如果现在终止读取循环
- 其他将其添加到向量中,然后打印整个向量
可以通过逐步改进来完成此操作序列。
步骤1.读取名称
首先,您需要能够连续阅读名称:
#include <iostream>
#include <string>
int main()
{
std::string name;
while (std::getline(std::cin, name))
std::cout << name << 'n';
}
很简单。运行此操作将回应您一次键入的任何字符串,并由新线隔开。
步骤2.在向量中累积名称
接下来,我们需要添加一个向量来保留我们正在阅读的字符串,并以" BART"的名称为" BART"。对于此通行证,我们将仅将我们阅读的所有字符串放入向量
中#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> names = { "Bart" };
std::string name;
while (std::getline(std::cin, name))
{
names.emplace_back(name);
for (auto const& s : names)
std::cout << s << ' ';
std::cout.put('n');
}
}
除了之前所做的事情外,我们现在正在累积矢量中的字符串,包括重复项,并在每个名称读取后报告矢量内容。这使我们更接近我们所陈述的目标。
步骤3:基于重复检测的条件循环退出
现在,我们需要检查重复项,并在循环发生后终止。我们可以使用std::find
来做到这一点。最终代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> names = { "Bart" };
std::string name;
while (std::getline(std::cin, name))
{
if (std::find(names.begin(), names.end(), name) != names.end())
break;
names.emplace_back(name);
for (auto const& s : names)
std::cout << s << ' ';
std::cout.put('n');
}
}
就是这样。这是一个简单的任务,但是它很好地借出了一个示例,说明如何将多部分任务分解为可管理的目标,然后将其构建成零件。
希望您发现它有用。
现在我的代码看起来像这样:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
bool isinVector (std::string uElement, std::vector<std::string> uArray) {
bool invector = false;
std::vector<std::string>::iterator it = std::find(uArray.begin(),
uArray.end(),uElement);
if(it != uArray.end()){
invector = true;
}
return invector;
}
int main(){
bool trigger = false;
std::string name;
std::vector<std::string> names = { "Bart" };
while (std::getline(std::cin, name)){
if (isinVector(name, names)) {
std::cout << "true" << std::endl;
break;
}
else
{
std::cout << "false" << std::endl;
names.emplace_back(name);
}
}
return 0;
}
它有效,非常感谢!
- 没有找到相关文章