2

I'm trying to create a dynamic 2d std::string vector but having trouble adding the new rows:

std::vector<std::vector<std::string>> hops_vector;

int Hop = 0;

for (Hop = 0; Hop < RouteHops; Hop++)
{
    char HopIPString[20];
    HopIPString[0] = 0;
    RouteTestGetHopTimedOut(TestHandle, Hop, &HopTimedOut);
    std::string thehop = std::to_string(Hop + 1);

    //If the hop hasn't been registered yet
    if (hopsstring.find(thehop) == std::string::npos) {
        vector<int> row; // Create an empty row
        hopsstring += thehop+","; // Add hop number to hop string

        //Add columns for pings per hop + IP and Loss/No Loss
        for (int j = 0; j < PingsPerHop+2; j++) {
            row.push_back(j); // Add an element (column) to the row
        }

        hops_vector.push_back(row); // Add the row to the main vector
        hops_vector[Hop][0] = thehop;
    }
}

The line hops_vector.push_back(row); gives me a no instance of overload error. I assume because hops_vector is an std::string vector. Changing it to an int solves that issue but then I can't add string to the vector!

halfer
  • 19,824
  • 17
  • 99
  • 186
Dan
  • 2,304
  • 6
  • 42
  • 69
  • 2
    `row` is a `vector`, it needs to be `std::vector` instead – UnholySheep Nov 04 '16 at 20:23
  • Also why are you adding integers into `row`? What is the purpose (as you clearly want strings)? – UnholySheep Nov 04 '16 at 20:25
  • @UnholySheep I now get the error on the row.push_back(j) line. As J is an int. So, how do I add columns to the newly create row without using an int? – Dan Nov 04 '16 at 20:25
  • I'm clearly misunderstanding what it's doing. very new to this – Dan Nov 04 '16 at 20:26
  • I think you could replace `vector row;` with `std::vector row(PingsPerHop+2, "");` and remove the `for` loop to achieve what you want – UnholySheep Nov 04 '16 at 20:27
  • 1
    @DanJamesPalmer Why at the end did you assume there is a `hops_vector[Hops]` that exists? What if `Hops` was 4, and there are less than 5 rows in the 2D vector? That would result in an out-of-bounds access. Maybe the 2D array isn't what you want, and what you really should use is a `std::map>`, where `string` is the hop number, and `vector` are the vector of integers associated with the string? That would make far more sense (and more efficient) than a 2D array and a call to `std::find` each time. – PaulMcKenzie Nov 04 '16 at 22:12
  • @Unholysheep add an answer and I'll accept it – Dan Nov 09 '16 at 22:01

1 Answers1

0

From the comments:

The simple way to fix the compiler error is to replace vector<int> row; with std::vector<std::string> row(PingsPerHop+2, ""); (which creates a vector filled with "empty" strings) and remove the for loop trying to fill it.

However as @PaulMcKenzie pointed out a different approach using a std::map<std::string, std::vector<int>>, with the string being the hop number and the vector holding the integers associated with it, is (most likely) a better approach (in both efficiency and conceptually)

UnholySheep
  • 3,967
  • 4
  • 19
  • 24