Home > Software design >  Transferring values in a vector into a structure in c
Transferring values in a vector into a structure in c

Time:01-19

I have a text file that contains values like

[1, 2]-3-Big_City
[1, 3]-3-Big_City
[2, 1]-3-Big_City
[2, 2]-3-Big_City
[2, 3]-3-Big_City
[2, 7]-2-Mid_City
[2, 8]-2-Mid_City
[3, 1]-3-Big_City
[3, 2]-3-Big_City
[3, 3]-3-Big_City
[3, 7]-2-Mid_City
[3, 8]-2-Mid_City
[7, 7]-1-Small_City

I managed to separate them into separate lines using a string tokenizer I found.

std::vector<std::string> tokenizeString(std::string input, std::string delimiter)
{
    size_t pos = 0;
    std::string token;
    std::vector<std::string> result;

    while ((pos = input.find(delimiter)) != std::string::npos)
    {
        token = input.substr(0, pos);
        result.push_back(token);
        input.erase(0, pos   delimiter.length());
    }

    result.push_back(input);

    return (result);
}

And this is the function where I called the string tokenizer


    filename = lineArray[2];
    std::fstream inputFile(filename.c_str(), std::fstream::in);

    std::cout << std::endl;
    std::cout << std::endl;

    std::string line;
    cityLoc city;

    while (getline(inputFile, line)) {
        std::vector<std::string> tokenStringVector = tokenizeString(line, "-");

        std::cout << std::endl;
        for (int i = 0; i < tokenStringVector.size(); i  )
            std::cout << tokenStringVector[i] << std::endl;
        std::cout << std::endl;
    }

    std::cout << std::endl;
}

The problem is that I'm not allowed to use vectors to store the value of tokenStringVector, and I can't figure out how to transfer the value of a vector into the struct city. All I could find was storing a vector in a struct which is not what I want. Any help will be appreciated.

Edit: Here's the structure of cityLoc, I don't really know how to store the coords so I decided to just go with a std::string since I figured I could just remove the brackets and comma afterwards.

struct cityLoc {
    std::string cityCoords;
    int cityId;
    std::string cityName;
};

CodePudding user response:

Since each line in your input file has fixed number of tokens ( here 3). Instead of using vector you can use an array of strings. And done. You can write tokenizer function like

void tokenizeString(std::string input, std::string delimiter, string tokenArray[])
{
    // Tokenize the string and put the tokens in tokenArray

}

And you can call this functions as below(just edit in your code)

 filename = lineArray[2];
    std::fstream inputFile(filename.c_str(), std::fstream::in);

    std::cout << std::endl;
    std::cout << std::endl;

    std::string line;
    int count = 0;
    cityLoc city;
    string tokenArray[5]; // Any length bigger than 3 would work because of the input file structure
    while (getline(inputFile, line)) {
        count  ;
        tokenizeString(line, "-", tokenArray);

        std::cout << std::endl;
        for (int i = 0; i < 3; i  )
            std::cout << tokenArray[i] << std::endl;
        std::cout << std::endl;
    }

    std::cout << std::endl;
}

CodePudding user response:

A line looks like

[1, 2]-3-Big_City

and is split into three elements, using - as separator

  • [1, 2]
  • 3
  • Big_City

This would map to the struct like

  • coords: [1, 2]
  • id: 3
  • name: Big_City

The tokenizer could then be

cityLoc tokenizeString(const std::string &input, const std::string &delimiter)
{
    size_t pos, start;
    std::string token;
    cityLoc result;

    pos = input.find(delimiter);
    result.cityCoords = input.substr(0, pos);

    start = pos   delimiter.size();
    pos = input.find(delimiter, start);
    result.cityId = std::stoi(input.substr(start, pos));

    start = pos   delimiter.size();
    pos = input.find(delimiter, start);
    result.cityName = input.substr(start, pos);

    return (result);
}
  •  Tags:  
  • Related