Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a vector in a vector

I am trying to use a for statement with a vector in a vector.

std::vector<std::string> array = {
    {"A", "a"},
    {"B", "b"},
    {"C", "c"}
};

for (std::string& a : array) {
    if (letter == a[1] || letter == a[0]) {
        std::cout << a[0] << ": " << a[1] << std::endl;
        break;
    }
}

I am new to C++ and cannot figure out why it gives me errors about this.


Edit:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error: error: invalid operands to binary expression ('std::string' (aka 'basic_string<char>') and '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' (aka 'char'))

>Solution :

I think {"A", "a"} is already a verctor, and this array varable type should be vector<vector<string>>.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading