我刚刚了解了重载运算符,并决定对向量重载>>。我不确定这是否是正确的方法,但是可以。
07401b.f395accd246ae52d
但是后来我意识到此方法仅适用于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;
}
,不适用于其他向量。如何使它通用并与任何向量一起使用?
答案 0 :(得分:1)
更改最少:
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;
}
请注意,您应该对代码进行许多改进:使用std::size_t
,使用std::vector::resize()
,避免在操作符内部使用std::cin/cout
,避免使用using namespace std;
,返回适当的代码类型,请避免使用以_
开头的标识符(即使此处有效)...加上在问题注释中告诉您的其余信息。
答案 1 :(得分:0)
我对此很开心。这是我的:
源文件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;
}
构建命令(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
测试文件test2.txt:
[1,99,2222]
["fred", "string with spaces", "", "another space string",
"string \"string\" string"]
测试命令:
$ ./json-style-vector-input <test2.txt
[1, 99, 2222]
["fred", "string with spaces", "", "another space string", "string \"string\" string"]