splitting a string in c++

with respect to below string, i need to split it and store the values accordingly as below,
P2P-DEVICE-FOUND fa:7b:7a:42:02:13 p2p_dev_addr=fa:7b:7a:42:02:13
pri_dev_type=1-0050F204-1 name='p2p-TEST1' config_methods=0x188 dev_capab=0x27 group_capab=0x0 wfd_dev_info=000006015d022a0032

dev_addr = fa:7b:7a:42:02:13
dev_type = 1-0050F204-1
dev_name = p2p-TEST1
config_method = 0x188
dev_capab = 0x27
group_capab = 0x0
dev_info = 000006015d022a0032

Please do help me in splitting the above string. I am confused about how to split it as above and store. Please help me. I am new to c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <vector>
#include <string>
#include <sstream>

std::vector<std::string> devSplit(const std::string &str) {
    std::istringstream iss(str);
    std::vector<std::string> vect;
    std::string word;
    
    static std::vector<std::string> values =
    {
        "dev_addr = ",
        "dev_type = ",
        "dev_name = ",
        "config_methods = ",
        "dev_capab = ",
        "group_capab = ",
        "dev_info = "
    };
    
    for(int v = 0; std::getline(iss, word, '='); v++) {
        iss >> word;
        vect.push_back(values[v] + word);
    }
    
    return vect;    
}


http://coliru.stacked-crooked.com/a/ca1e1dc5241bdf31
Last edited on
actually this is the string :
P2P-DEVICE-FOUND fa:7b:7a:42:02:13 p2p_dev_addr=fa:7b:7a:42:02:13
pri_dev_type=1-0050F204-1 name='p2p-TEST1' config_methods=0x188 dev_capab=0x27 group_capab=0x0 wfd_dev_info=000006015d022a0032

and i have below variables stored in a class called wifip2pdevice,
dev_addr
dev_type
dev_name
config_method
dev_capab
group_capab
dev_info

I have to split the above string and store the values alone in the above mentioned variables
Last edited on
Hi Smac89,

I have executed your program in codeblocks. I got the following errors,

Could you please help me out in resolving the errors.

G:\C practicing\SplittingStrgs.cpp||In function 'std::vector<std::basic_string<char> > devSplit(const string&)':|
G:\C practicing\SplittingStrgs.cpp|20|error: in C++98 'values' must be initialized by constructor, not by '{...}'|
G:\C practicing\SplittingStrgs.cpp|20|error: could not convert '{"dev_addr = ", "dev_type = ", "dev_name = ", "config_methods = ", "dev_capab = ", "group_capab = ", "dev_info = "}' from '<brace-enclosed initializer list>' to 'std::vector<std::basic_string<char> >'|
G:\C practicing\SplittingStrgs.cpp||In function 'int main()':|
G:\C practicing\SplittingStrgs.cpp|33|error: range-based 'for' loops are not allowed in C++98 mode|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|


Thanks in advance.
go to project / build options then check the -std=c++11 check box
"Build options.." is in disable mode
Use something like this if your compiler does not support c++11:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <vector>
#include <string>
#include <sstream>
#include <iostream>

std::vector<std::string> devSplit(const std::string &str)
{
    std::istringstream iss(str);
    std::vector<std::string> vect;
    std::string word;

    /*static*/ std::vector<std::string> values ;
    values.push_back ("dev_addr = ");
    values.push_back ("dev_type = ");
    values.push_back ("dev_name = ");
    values.push_back ("config_methods = ");
    values.push_back ("dev_capab = ");
    values.push_back ("group_capab = ");
    values.push_back ("dev_info = ");


    for(int v = 0; std::getline(iss, word, '='); v++)
    {
        iss >> word;
        vect.push_back(values[v] + word);
    }

    return vect;
}


int main()
{

    std::vector <std::string> v = devSplit("P2P-DEVICE-FOUND fa:7b:7a:42:02:13 "
                                           "p2p_dev_addr=fa:7b:7a:42:02:13 "
                                           "pri_dev_type=1-0050F204-1 "
                                           "name='p2p-TEST1' config_methods=0x188 "
                                           "dev_capab=0x27 group_capab=0x0 "
                                           "wfd_dev_info=000006015d022a0032");
    for (size_t i = 0; i < v.size(); i++)
    {
        std::cout << v[i] << std::endl;
    }


    return 0;
}


It is equivalent with original code posted above.
Topic archived. No new replies allowed.