Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何将向量解析成C++中的布尔?_C++_String_Vector_Boolean - Fatal编程技术网

我如何将向量解析成C++中的布尔?

我如何将向量解析成C++中的布尔?,c++,string,vector,boolean,C++,String,Vector,Boolean,我正试图编写一个程序,首先检查一个名字是否在向量中,如果不是,然后将它添加到向量中。我的代码似乎在解析方面有困难,至少我从中得到了一些帮助。我试着把字符串改成一个字符,但没有多大帮助 #include <iostream> #include <vector> #include <string> bool isinVector(std::string uElement, std::vector<std::string> uArray) {

我正试图编写一个程序,首先检查一个名字是否在向量中,如果不是,然后将它添加到向量中。我的代码似乎在解析方面有困难,至少我从中得到了一些帮助。我试着把字符串改成一个字符,但没有多大帮助

#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,则该元素存在。此外,对于循环使用了sizeof,这是不正确的,请使用vector.size方法。你一直在循环,直到在更新的帖子中出现错误

sizeof的不当使用 重塑标准算法 缺少错误检查 考虑一下你试图完成的任务。您想:

初始化包含名称Bart的起始向量 不断读新名字。对于每个新名称,请阅读:

a。检查它是否已经在向量中

如果存在,则终止读取循环 否则将其添加到向量,并打印整个向量 这一系列操作可以通过逐步细化来完成

第一步。读名字

首先,您需要能够连续读取姓名:

#include <iostream>
#include <string>

int main()
{
    std::string name;
    while (std::getline(std::cin, name))
        std::cout << name << '\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;
}

这很有效,谢谢大家

请明确你到底需要什么帮助。这是相当模糊的现在。请使标题相关的问题,你有不只是请帮助我。代码的哪一部分出错了?请缩小范围。sizeofuArray不提供uArray的大小。改用uArray.size.sizeof并不代表您认为它的意思,您在任何示例中都没有看到它与向量一起使用。谢谢,下次我会更加小心地写下我的示例代码!如果您决定将语法更改为+11,我将在主函数中添加lambda表达式,并编写auto而不是iterator类型。OP可能不熟悉+03/+11语法,解释您的更改会对他有帮助好的,我会进行调整。通过.at方法而不是索引[]访问向量中的元素更安全-当然,但是当您知道索引在范围内时,.at只会增加编译器无法为您删除的不必要的开销。是@JesperJuhl,这是真的。理想情况下,如果开发人员不知道向量的边界,则不应调用索引或.at。他们应该永远知道。我可以看到在一些情况下,在多线程应用程序中,向量正在被更改,而开发人员在特定时间可能不知道大小。但在这种情况下,可以使用线程锁定。在一个单线程应用程序中,我不认为我遇到过不知道自己向量的边界的情况。如果你想看的话,我确实对最初发布的代码做了进一步的调整。还可以看看@WhozCraig post,里面也有有用的信息。
#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;
}