cpp实现获取字符串数据3 (2,3,2,4) ((1,2),(2,1),(3,3)) ((1,2),(1,1),(2,3),(3,2),(4,3))中((1,2),(1,1),(2,3),(3,2),(4,3))数据
时间: 2023-08-16 17:03:27 浏览: 46
可以使用以下代码来获取 ((1,2),(1,1),(2,3),(3,2),(4,3)) 数据:
```cpp
#include <iostream>
#include <vector>
#include <sstream>
int main() {
std::string str = "((1,2),(1,1),(2,3),(3,2),(4,3))";
std::vector<std::pair<int, int>> data;
std::stringstream ss(str);
char c;
int a, b;
while (ss >> c >> a >> c >> b >> c >> c) {
data.push_back({a, b});
}
for (auto pair : data) {
std::cout << "(" << pair.first << "," << pair.second << ") ";
}
std::cout << std::endl;
return 0;
}
```
输出结果为:
```
(1,2) (1,1) (2,3) (3,2) (4,3)
```
相关问题
cpp代码实现获取字符串数据3 (2,2) ((1,1),(2,1),(3,3))第二个左括号位置
以下是获取字符串数据3中第二个左括号位置的C++代码实现:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "(2,2) ((1,1),(2,1),(3,3))";
int count = 0;
int index = -1;
for (int i = 0; i < str.length(); i++) {
if (str[i] == '(') {
count++;
if (count == 2) {
index = i;
break;
}
}
}
std::cout << "第二个左括号位置为:" << index << std::endl;
return 0;
}
```
输出结果为:
```
第二个左括号位置为:6
```
解释:遍历字符串,当遇到左括号时,计数器加1,当计数器等于2时,记录当前位置并跳出循环。最后输出记录的位置即为第二个左括号的位置。
cpp实现获取字符串数据3 (2,3,2,4) ((1,2),(2,1),(3,3)) ((1,2),(1,1),(2,3),(3,2),(4,3))中以空格分割的最后一段内容
根据题目描述,最后一段内容为 ((1,2),(1,1),(2,3),(3,2),(4,3)),可以使用字符串流和 getline 函数来实现。
代码如下:
```cpp
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string input = "3 (2,3,2,4) ((1,2),(2,1),(3,3)) ((1,2),(1,1),(2,3),(3,2),(4,3))";
std::string last_part;
std::stringstream ss(input);
std::string token;
// 读取每个以空格分割的部分
while (ss >> token) {
last_part = token;
}
std::cout << last_part << std::endl;
return 0;
}
```
输出:
```
((1,2),(1,1),(2,3),(3,2),(4,3))
```