简体   繁体   中英

Universal operator >> for std::vector

I just learned about overloading operators and decided to overload operator >> for std::vector . I'm not sure if this is the correct way to do it but it works.

void operator >> (istream &in, vector<int> &_vector)
{
    int vectorSize;
    cout << "Enter vector size\n";
    cin >> vectorSize;

    _vector = vector<int>(vectorSize);

    cout << "Enter the array\n";
    for (int &element : _vector)
        in >> element;
}

But then I realized that this method works only for vector<int> and doesn't work for other vectors. How can I make it universal and work with any vector?

With minimal changes:

template <class T>
void operator >> (istream &in, vector<T> &_vector)
{
    int vectorSize;
    cout << "Enter vector size\n";
    cin >> vectorSize;

    _vector = vector<T>(vectorSize);

    cout << "Enter the array\n";
    for (T &element : _vector)
        in >> element;
}

Note that there are many improvements you should do to the code: use std::size_t , use std::vector::resize() , avoid std::cin/cout inside an operator, avoid using namespace std; , return the appropriate type, avoid identifiers that start with _ (even if valid here)... Plus the rest that they told you in the question comments, too.

I had some fun with this. Here's mine:

Source file json-style-vector-input.cpp:

#include <vector>
#include <iostream>
#include <utility>
#include <string_view>
#include <string>

using namespace std::literals;

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T> &v) {
    std::string_view separator = "";
    constexpr std::string_view comma = ", ";
    os << '[';
    for(const auto &i: v) {
        os << separator;
        separator = comma;
        os << i;
    }
    os << ']';
    return os;
}

template<typename T>
std::istream& operator>>(std::istream& is, std::vector<T> &v) {
    char c;
    is >> c;
    if(c != '[') {
        is.setstate(std::ios::failbit);
        return is;
    }
    v.clear();
    bool done = false;
    while(!done) {
        T tmp;
        is >> tmp;
        if(!is) return is;
        v.push_back(std::move(tmp));
        is >> c;
        if(!is) return is;
        if(c == ']') {
            done = true;
        } else if(c != ',') {
            is.setstate(std::ios::failbit);
            done = true;
        }
    }

    return is;
}

std::ostream& operator<<(std::ostream &os, const std::ios::iostate &state) {
    using std::ios;
    if(state == ios::goodbit) os << "goodbit"sv;
    if((state & ios::badbit) == ios::badbit) os << "badbit"sv;
    if((state & ios::failbit) == ios::failbit) os << "failbit"sv;
    if((state & ios::eofbit) == ios::eofbit) os << "eofbit"sv;
    return os;
}

template<typename Ch = char>
class quoted_string : public std::basic_string<Ch> {
};

template<typename Ch>
std::istream& operator>>(std::istream &is, quoted_string<Ch> &s) {
    char c;
    is >> c;
    if(!is) return is;
    if(c != '"') {
        is.setstate(std::ios::failbit);
        return is;
    }
    std::getline(is, s, '"');
    while(s.length() > 0 && s[ s.length()-1 ] == '\\') {
        s[ s.length()-1 ] = '"';
        std::basic_string<Ch> more;
        std::getline(is, more, '"');
        s += more;
    }
    return is;
}

template<typename Ch>
std::ostream& operator<<(std::ostream &os, const quoted_string<Ch> &s) {
    os << '"';
    if( s.find('"') != std::basic_string<Ch>::npos ) {
        for( const auto& c: s ) {
            if( c == '"' ) os << '\\';
            os << c;
        }
    } else {
        os << static_cast<std::basic_string<Ch>>(s);
    }
    os << '"';
    return os;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::vector<int> a;
    std::cin >> a;
    if(!std::cin) {
        std::cout << "input failure: "sv << std::cin.rdstate() << std::endl;
        std::cin.clear();
    }
    std::cout << a << std::endl;

    std::vector<quoted_string<char>> b;
    std::cin >> b;
    if(!std::cin) {
        std::cout << "input failure: "sv << std::cin.rdstate() << std::endl;
        std::cin.clear();
    }
    std::cout << b << std::endl;
    return 0;
}

Build command (Linux):

g++ -Wall -W -pedantic -g -pthread -O3 -flto -fno-fat-lto-objects  -std=c++17   -pthread  json-style-vector-input.cpp   -o json-style-vector-input

Test file test2.txt:

[1,99,2222]
["fred", "string with spaces", "", "another space string",
"string \"string\" string"]

Test command:

$ ./json-style-vector-input <test2.txt 
[1, 99, 2222]
["fred", "string with spaces", "", "another space string", "string \"string\" string"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM