Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include <string>
- std::vector<std::string> parsable_data{ "123", "abc", "xyz" };
- //As a matrix, "parsable_data" would be represented
- //like this:
- /*
- R0 R1 R2
- C0 "123" "abc" "xyz"
- */
- //Make the function convert it to an
- //"std::vector<std::vector<std::string>>"
- //thats represented like this:
- /*
- R0 R1 R2
- C0 "1" "2" "3"
- C1 "a" "b" "c"
- C2 "x" "y" "z"
- */
- void ParseAsMatrix(std::vector<std::string> data_to_parse) {
- size_t index = 0;
- std::vector<std::vector<std::string>> parsed_matrix;
- for (auto i : parsed_data) {
- size_t str_index = 0;
- std::string curr_str = parsed_data[index];
- for (std::string::iterator str_it = curr_str.begin(); str_it != curr_str.end(); str_it++) {
- std::string str = curr_str[str_index];
- parsed_matrix[index][str_index] = str;
- std::cout << "parsed_matrx[" << index << "][" << str_index << "] = " << str << "\n";
- str_index++;
- }
- std::cout << "\n";
- index++;
- }
- }
- /*
- Expected Output:
- parsed_matrix[0][0] = 1
- parsed_matrix[0][1] = 2
- parsed_matrix[0][2] = 3
- parsed_matrix[1][0] = a
- parsed_matrix[1][1] = b
- parsed_matrix[1][2] = c
- parsed_matrix[2][0] = x
- parsed_matrix[2][1] = y
- parsed_matrix[2][2] = z
- */
- int main() {
- ParseAsMatrix(parsable_data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement