0

I have String vector that has data set look like below.

vector<string> result;

<index> | <Name> | <email> | <status>

    1|duleep|dfe@gamil.com|0
    2|dasun|dasun@da.com|0
    3|sampath|lkdf@dg.lk|1
    4|Nuwan|Kandyjkj@lkj.com|0

now i want to get separate vector data(Name, Index,Status) please suggest best way to do this using C++(how can i convert to string array[4][4]?)

user881703
  • 1,111
  • 3
  • 19
  • 38

1 Answers1

1

Here is an example I came up with using boost::tokenizer (if you don't want to use boost, then sorry):

#include <iostream>
#include <boost/tokenizer.hpp>
#include <string>
#include <vector>
int main()
{
  std::vector<std::string> v;
  v.push_back("1|duleep|dfe@gamil.com|0");
  v.push_back("2|dasun|dasun@da.com|0");
  v.push_back("3|sampath|lkdf@dg.lk|1");
  v.push_back("4|Nuwan|Kandyjkj@lkj.com|0");

  boost::char_separator<char> sep("|");
  std::vector<boost::tokenizer<boost::char_separator<char>>> tokens;
  for (auto& s : v)
  {
    tokens.push_back({s, sep});
  }
}

If you want to use std::string array[4][4], just iterate through the tokens and assign them to your array.

Here is another way without boost:

  for (auto& s : v)
  {
    std::stringstream ss(s);
    std::string token;
    while (std::getline(ss, token, '|'))
    {
        // Put token into your array here
    }
  }
Jesse Good
  • 50,901
  • 14
  • 124
  • 166