Why can't I use cout to print an array of string values ​​in C ++?

This is a piece of code from a fairly simple program that I am writing. I am new to C ++, but have experience in Java, so I may have a biased understanding of how to print values. My problem is when I do this line:

cout << "Please enter the weight for edge " << verticies[i] << endl;

I get an error saying that the operands do not match the specified statement for <<. This basically says that I cannot do cout <vertices, [I].

Why is this happening?

Here is the code:

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    string verticies[6] = { "AB", "AC", "AD", "BC", "BD", "CD" };
    int edges[6];

    for (int i = 0; i < 6; i++)
    {
        cout << "Please enter the weight for edge " << verticies[i] << endl;
    }

    system("PAUSE");

    return 0;
}
+4
source share
2 answers

Try turning it on <string>, should be enough

+8
source

<string>, std::basic_string, std::string

, operator <<.

std:: map insted .

std::map<std::string, int> verticies = 
{ 
   { "AB", 0 }, { "AC", 0 }, { "AD", 0 }, { "BC", 0 }, { "BD", 0 }, { "CD", 0 } 
};

, std:: pair .

{ std::pair<std::string, int>( "AB", 0 ), std::pair<std::string, int>( "AC", 0 ), ...}
+3

Source: https://habr.com/ru/post/1535937/


All Articles