0

How to take delimiter as -0 between array size and array value of integer in c++.

for example: 2 -0 1,2 -0
array-size '-0' 1st array values separated by "," '-0'

If I take input in a string using get line then how to separate size from the string and take input of array.

int main(){

string s,t;

getline(cin,s);

stringstream x(s);

while(getline(x,t,"-0")){
    cout << t;
}

}

I cannot understand delimiter concept properly from google, can you explain it and take input in the: " array-size '-0' 1st array values separated by ',' '-0' " form.

Rahul
  • 13
  • 5
  • 1
    I'm having the greatest difficulty understanding what you want to achieve. Can you give some context? What's an array size of -0? – JHBonarius Oct 17 '20 at 19:04
  • Does this answer your question? [Parse (split) a string in C++ using string delimiter (standard C++)](https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c) – moooeeeep Oct 18 '20 at 12:37

1 Answers1

0

Given the input, the easiest way is to check the string token as being the -0 delimiter.

#include <iostream>
#include <sstream>
#include <string>

using std::cin;
using std::cout;
using std::getline;
using std::string;
using std::stringstream;

int main(){
    string s,t;
    getline(cin, s);
    stringstream x(s);
    char const* sep = "";
    while (x >> t) {
        if (t == "-0") {
            cout << sep << "<DELIMITER>";
        } else {
            cout << sep << t;
        }
        sep = " ";
    }
    cout << "\n";
}

That gives:

echo '2 -0 1,2 -0' | ./a.out
2 <DELIMITER> 1,2 <DELIMITER>

Update

Storing the values into a std::vector<int>.

#include <cstddef>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::getline;
using std::ostream;
using std::runtime_error;
using std::size_t;
using std::stoi;
using std::string;
using std::stringstream;
using std::vector;

static auto MakeVecInt(string line) -> vector<int> {
    auto result = vector<int>();
    auto ss = stringstream(line);
    auto value = string{};

    while(getline(ss, value, ',')) {
        result.push_back(stoi(value));
    }

    return result;
}

static auto operator<<(ostream& out, vector<int> const& v) -> ostream& {
    char const* sep = "";

    for (auto i : v) {
        out << sep << i;
        sep = " ";
    }

    return out;
}

int main() {
    auto line = string{};
    getline(cin, line);
    auto ss = stringstream(line);

    auto count = size_t{};
    auto delimiter1 = string{};
    auto values = string{};
    auto delimiter2 = string{};

    if (!(ss >> count >> delimiter1 >> values >> delimiter2))
        throw runtime_error("bad input");

    if (delimiter1 != "-0" || delimiter2 != "-0")
        throw runtime_error("bad input");

    auto vec = MakeVecInt(values);

    if (count != vec.size())
        throw runtime_error("bad input");

    cout << vec << "\n";
}

Update

Note in the above code the vector<int> output routine:

static auto operator<<(ostream& out, vector<int> const& v) -> ostream&

It outputs each element in the vector, with a space between each element. It does not output a space before the first element.

Eljay
  • 3,156
  • 2
  • 12
  • 21
  • Please don't make your life harder than it needs to be. Those using declarations and that function declaration syntax just needlessly complicates the provided example. Or is this effect intended? – moooeeeep Oct 18 '20 at 12:47
  • @moooeeeep • It is intentional. I prefer to post complete examples, not snippets that presume C++ novices will be able to fill in the blanks themselves. – Eljay Oct 18 '20 at 12:50