在C++中用定界符-1分隔数组的大小和数组值
separate the size of array and array value by delimiter -1 in c++
Separate the size of array and array value by delimiter -1
Input is of the form:
Array-size -1 array-values(separated by ,) -1 number -1
3 -1 3,5,7 -1 6 -1
Don't use vector, take the values into array of size given by user otherwise print "-1".
我试过这段代码,但我无法完成它:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s{};
getline(cin, s)
stringstream ss(s);
string count = size_t{};
string delimiter1 = string{};
string value = string{};
string delimiter2 = string{};
string size = string{};
string delimiter3 = string{};
if(!(ss >> count >> delimiter1 >> values >> delimiter2 >> size >> delimiter2))
cout << -1 << endl;
if(delimiter1 != "-1" || delimiter2 != "-1" || delimiter3 != "-1")
cout << -1 << endl;
int a[1000];
while(getline(ss, value, ',')){
a.push_back(stoi(value));
}
}
你的代码似乎没有一百万英里远,但你没有 decalred values
,并且你在错误的地方初始化了 ss
,并在其中使用了 delimiter2
你的意思是 delimiter3
,你试图在数组上使用 push_back
。
其他错误是将整数值读取为字符串,这似乎为您自己做了更多工作。
最后,既然您知道必须读取多少个整数,您或许应该使用该信息。
这段代码看起来更接近
int main() {
// read line of data
string s;
getline(cin, s)
// initial parse of data
size_t count, size;
int delimiter1, delimiter2, delimiter3;
string values;
if (!(ss >> count >> delimiter1 >> values >> delimiter2 >> size >> delimiter3))
std::cerr << "error here\n";
// check delimiters
if (delimiter1 != -1 || delimiter2 != -1 || delimiter3 != -1)
std::cerr << "error here\n";
// read count integers into array
int a[1000];
stringstream ss(values);
string value;
for (size_t i = 0; i < count; ++i)
if (!getline(ss, value, ',')) {
std::cerr << "error here\n";
}
a[i] = stoi(value);
}
}
我觉得这么多代码就可以了。如果输入格式错误或 n
非常大,它将在 stderr
上打印 -1
。
#include <iostream>
#include <istream>
#include <string>
int main() {
try {
int n, _, number;
std::string str;
std::cin.exceptions(std::istream::failbit);
std::cin >> n >> _;
int *arr = new int[n];
for (int i = 0, temp; i < n; ++i) {
std::cin >> temp;
arr[i] = temp;
std::cin >> std::ws;
std::cin.ignore();
}
std::cin >> _ >> number >> _;
// do something with array and number
for (int i = 0; i < n; ++i)
std::cout << arr[i] << ' ';
std::cout << '\n' << number;
} catch (...) {
std::cerr << -1;
}
}
Separate the size of array and array value by delimiter -1
Input is of the form:
Array-size -1 array-values(separated by ,) -1 number -1 3 -1 3,5,7 -1 6 -1
Don't use vector, take the values into array of size given by user otherwise print "-1".
我试过这段代码,但我无法完成它:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s{};
getline(cin, s)
stringstream ss(s);
string count = size_t{};
string delimiter1 = string{};
string value = string{};
string delimiter2 = string{};
string size = string{};
string delimiter3 = string{};
if(!(ss >> count >> delimiter1 >> values >> delimiter2 >> size >> delimiter2))
cout << -1 << endl;
if(delimiter1 != "-1" || delimiter2 != "-1" || delimiter3 != "-1")
cout << -1 << endl;
int a[1000];
while(getline(ss, value, ',')){
a.push_back(stoi(value));
}
}
你的代码似乎没有一百万英里远,但你没有 decalred values
,并且你在错误的地方初始化了 ss
,并在其中使用了 delimiter2
你的意思是 delimiter3
,你试图在数组上使用 push_back
。
其他错误是将整数值读取为字符串,这似乎为您自己做了更多工作。
最后,既然您知道必须读取多少个整数,您或许应该使用该信息。
这段代码看起来更接近
int main() {
// read line of data
string s;
getline(cin, s)
// initial parse of data
size_t count, size;
int delimiter1, delimiter2, delimiter3;
string values;
if (!(ss >> count >> delimiter1 >> values >> delimiter2 >> size >> delimiter3))
std::cerr << "error here\n";
// check delimiters
if (delimiter1 != -1 || delimiter2 != -1 || delimiter3 != -1)
std::cerr << "error here\n";
// read count integers into array
int a[1000];
stringstream ss(values);
string value;
for (size_t i = 0; i < count; ++i)
if (!getline(ss, value, ',')) {
std::cerr << "error here\n";
}
a[i] = stoi(value);
}
}
我觉得这么多代码就可以了。如果输入格式错误或 n
非常大,它将在 stderr
上打印 -1
。
#include <iostream>
#include <istream>
#include <string>
int main() {
try {
int n, _, number;
std::string str;
std::cin.exceptions(std::istream::failbit);
std::cin >> n >> _;
int *arr = new int[n];
for (int i = 0, temp; i < n; ++i) {
std::cin >> temp;
arr[i] = temp;
std::cin >> std::ws;
std::cin.ignore();
}
std::cin >> _ >> number >> _;
// do something with array and number
for (int i = 0; i < n; ++i)
std::cout << arr[i] << ' ';
std::cout << '\n' << number;
} catch (...) {
std::cerr << -1;
}
}