从字符串中提取整数并形成一个数组

Extract integer from string and form an array

本文关键字:数组 一个 字符串 提取 整数      更新时间:2023-10-16

从我学校C++开始,它看起来比Python更令人生畏!希望有人可以指导我。

我创建了一个简单的用户 I/O 来练习从用户输入中提取整数并根据输入形成数组。见下文:

#include <iostream>
#include <cstring>
using namespace std;
int main() {
int number_array = {};
string user_input;
cout << "Enter your array range: ";
cin >> user_input;
cout << "You have entered: " + user_input << endl;
return 0;
}

输入输入时,用户需要遵守此格式

x-y or x - y (e.g 0-5 or 0 - 5)

我脑子里有这个,我相信它会起作用,但我无法将其转化为C++。

  1. 程序将从用户输入中提取第一个和最后一个整数,"-"将被删除。我认为正则表达式可以做到这一点,但不太确定如何编码。
  2. 基于第一个和最后一个整数,它将在number_array中形成一个整数数组。例如,0-5 将形成 [0, 1, 2, 3, 4, 5],-4--1 将形成 [-4, -3, -2, -1]。我假设这里需要使用for 循环

>cin >> string将停止在空格处,所以让我们使用 std::getline 它将获取一整行输入。

正则表达式当然是实现此目的的一种方式:

法典

#include <iostream>
#include <cstring>
#include <regex>
int main() {
int number_array = {};
std::string user_input;
std::cout << "Enter your array range: ";
std::getline(std::cin, user_input);
std::cout << "You have entered: " + user_input << "n";
std::smatch m;
std::regex r(R"(^(d+) *- *(d+)$)");
if (!regex_match(user_input, m, r)) {
std::cout << "Didn't match regex!n";
return 1;
}
int start = std::stoi(m[1]);
int end = std::stoi(m[2]);
for (int i=start; i<=end; i++) {
std::cout << i << " ";
}
std::cout << "n";
return 0;
}

输出

> clang++-7 -pthread -std=c++17 -o main main.c
pp
> ./main
Enter your array range: 1 -- 4
You have entered: 1 -- 4
Didn't match regex!
exit status 1
> ./main
Enter your array range: 4 -  10
You have entered: 4 -  10
4 5 6 7 8 9 10 

https://repl.it/repls/FunctionalGiantChapters

话虽如此

由于您正在解析非常简单的东西,因此您也可以只使用:

fscanf("%d - %d", &start, &end)并忽略正则表达式的想法。

对于"解析",您可以执行以下操作:

char c1,c2,minus;
if ((cin >> c1 >> minus >> c2) && (minus == '-') &&
isdigit(c1) && isdigit(c2))
...ok case...
else
...nok case...

当数字只是一个数字时,做更复杂的事情是没有用

的对于"确定情况"中的数组,您可以执行

std::vector<int> number_array;
for (int i = c1; i <= c2; ++i)
number_array.push_back(i - '0');

例:

#include <iostream>
#include <vector>
#include <ctype.h>
int main()
{
char c1,c2,minus;
if ((std::cin >> c1 >> minus >> c2) && (minus == '-') &&
isdigit(c1) && isdigit(c2)) {
std::vector<int> number_array;

for (int i = c1; i <= c2; ++i)
number_array.push_back(i - '0');

// check
for (auto v: number_array)
std::cout << v << ' ';
std::cout << std::endl;
}
else
std::cerr << "invalid input" << std::endl;
}

编译和执行:

pi@raspberrypi:~ $ g++ -Wall c.cc
pi@raspberrypi:~ $ ./a.out
0-5
0 1 2 3 4 5 
pi@raspberrypi:~ $ ./a.out
0 - 5
0 1 2 3 4 5 
pi@raspberrypi:~ $ ./a.out
0- 5
0 1 2 3 4 5 
pi@raspberrypi:~ $ ./a.out
0 -5
0 1 2 3 4 5 
pi@raspberrypi:~ $ 
<小时 />

编辑后

最后读取 2 个整数而不仅仅是 2 位数字,使用scanf作为另一个答案提出的可能是更简单的解决方案:

#include <iostream>
#include <vector>
int main()
{
int v1, v2;

if (scanf("%d - %d", &v1, &v2) == 2) {
std::vector<int> number_array;

while (v1 <= v2)
number_array.push_back(v1++);

// check
for (auto v: number_array)
std::cout << v << ' ';
std::cout << std::endl;
}
else
std::cerr << "invalid input" << std::endl;
}

编译和执行:

pi@raspberrypi:~ $ g++ -Wall c.cc
pi@raspberrypi:~ $ ./a.out
0-5
0 1 2 3 4 5 
pi@raspberrypi:~ $ ./a.out
0 -5
0 1 2 3 4 5 
pi@raspberrypi:~ $ ./a.out
0 - 5
0 1 2 3 4 5 
pi@raspberrypi:~ $ ./a.out
0- 5
0 1 2 3 4 5 
pi@raspberrypi:~ $

这是做你想做的事的简单方法。

首先读取输入:

int low, high;
char dash; 
std::cin >> low >> dash >> high;

然后生成所需的范围:

std::vector<int> v(high - low + 1);
std::iota(v.begin(), v.end(), low);

这是一个演示。

相关文章: