Advertisement
Guest User

Challenge - Convert vec<str> to vec<vec<str>>

a guest
Nov 16th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5.  
  6.  
  7. std::vector<std::string> parsable_data{ "123", "abc", "xyz" };
  8.  
  9. //As a matrix, "parsable_data" would be represented
  10. //like this:
  11. /*
  12.  
  13. R0 R1 R2
  14.  
  15. C0 "123" "abc" "xyz"
  16.  
  17. */
  18.  
  19. //Make the function convert it to an
  20. //"std::vector<std::vector<std::string>>"
  21. //thats represented like this:
  22. /*
  23.  
  24. R0 R1 R2
  25.  
  26. C0 "1" "2" "3"
  27. C1 "a" "b" "c"
  28. C2 "x" "y" "z"
  29.  
  30. */
  31.  
  32.  
  33.  
  34. void ParseAsMatrix(std::vector<std::string> data_to_parse) {
  35. size_t index = 0;
  36. std::vector<std::vector<std::string>> parsed_matrix;
  37.  
  38. for (auto i : parsed_data) {
  39. size_t str_index = 0;
  40. std::string curr_str = parsed_data[index];
  41.  
  42. for (std::string::iterator str_it = curr_str.begin(); str_it != curr_str.end(); str_it++) {
  43. std::string str = curr_str[str_index];
  44. parsed_matrix[index][str_index] = str;
  45.  
  46. std::cout << "parsed_matrx[" << index << "][" << str_index << "] = " << str << "\n";
  47. str_index++;
  48. }
  49. std::cout << "\n";
  50. index++;
  51. }
  52. }
  53.  
  54. /*
  55. Expected Output:
  56. parsed_matrix[0][0] = 1
  57. parsed_matrix[0][1] = 2
  58. parsed_matrix[0][2] = 3
  59.  
  60. parsed_matrix[1][0] = a
  61. parsed_matrix[1][1] = b
  62. parsed_matrix[1][2] = c
  63.  
  64. parsed_matrix[2][0] = x
  65. parsed_matrix[2][1] = y
  66. parsed_matrix[2][2] = z
  67. */
  68.  
  69. int main() {
  70. ParseAsMatrix(parsable_data);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement