优化代码 stringstream转为char类型char str[PRO_LINE_SIZE]; stringstream ss; str = ss;
时间: 2023-07-15 09:15:19 浏览: 43
要将 stringstream 转换为 char 类型,可以使用 stringstream 的 str() 函数将其转换为 string 类型,再使用 c_str() 函数将其转换为 char 类型。具体代码如下:
```c++
char str[PRO_LINE_SIZE];
stringstream ss;
// 往 ss 中写入数据
...
// 将 ss 转换为 char 数组
strcpy(str, ss.str().c_str());
```
这里我们先定义了一个 char 数组 `str`,然后创建了一个 stringstream 对象 `ss`,往其中写入了数据。最后使用 `ss.str().c_str()` 将 `ss` 转换为 char 数组,并使用 `strcpy()` 函数将其拷贝到 `str` 数组中。需要注意的是,由于 `c_str()` 函数返回值是 const char*,因此我们需要使用 strcpy() 函数来将其拷贝到 char 数组中。
相关问题
char数组按行转vector数组
可以使用stringstream和getline函数来实现char数组按行转vector数组的操作。
示例代码如下:
```cpp
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
vector<string> charToVector(char* str) {
vector<string> res;
stringstream ss(str);
string line;
while (getline(ss, line)) {
res.push_back(line);
}
return res;
}
int main() {
char str[] = "hello\nworld\n";
vector<string> res = charToVector(str);
for (string s : res) {
cout << s << endl;
}
return 0;
}
```
输出:
```
hello
world
```
在上面的代码中,我们首先定义一个char类型的字符串str,然后将其传入charToVector函数中。在charToVector函数中,我们定义一个vector类型的res,并使用stringstream将char数组转换为stringstream类型。然后,我们使用getline函数将stringstream中的数据按行读取,并将其存储到vector中。最后,我们返回vector数组并在主函数中输出。
下面代码转化为c++代码 import java.util.*; public class Main { public static void main(String args[]) { Scanner scan = new Scanner(System.in); while (scan.hasNextLine()) { String company = scan.nextLine().trim(); String team = scan.nextLine().trim(); String result = scan.nextLine().trim(); String[] results = result.split(","); System.out.println(getResult(company,team,results)); } } private static String getResult(String company, String team, String[] results) { if(!results[0].equalsIgnoreCase(company) || !results[2].equalsIgnoreCase(team)){ return "Not Found"; } return parseScore(company, team, results[1]); } private static String parseScore(String company, String team, String result) { String[] resultItems = result.split("_"); String people = resultItems[0]; String score = resultItems[1]; String evaluate = resultItems[2]; StringBuilder sb = new StringBuilder(); sb.append(company.toUpperCase()) .append(",").append(people) .append("_").append(score) .append("_").append(convertResult(score,evaluate)) .append(",").append(team); return sb.toString(); } private static String convertResult(String score, String evaluate){ String replace = Integer.parseInt(score.trim()) >= 80 || isTop3(evaluate) ? "perfect" : "better"; int index1 = evaluate.indexOf("{"); int index2 = evaluate.indexOf("}"); return evaluate.substring(0,index1) + replace + evaluate.substring(index2 + 1); } private static boolean isTop3(String evaluate){ String sort = ""; char[] evaluates = evaluate.toCharArray(); for (int i =0; i < evaluates.length; i++){ char temp = evaluates[i]; if(!Character.isDigit(temp)){ break; } sort += Character.toString(temp); } return Integer.parseInt(sort) <= 3; } }
下面是将Java代码转换为C++的等效代码:
```cpp
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
string getResult(string company, string team, vector<string>& results) {
if (results[0] != company || results[2] != team) {
return "Not Found";
}
return parseScore(company, team, results[1]);
}
string parseScore(string company, string team, string result) {
stringstream ss(result);
string people, score, evaluate;
getline(ss, people, '_');
getline(ss, score, '_');
getline(ss, evaluate, '_');
stringstream output;
output << company << "," << people << "_" << score << "_" << convertResult(score, evaluate) << "," << team;
return output.str();
}
string convertResult(string score, string evaluate) {
int scoreValue = stoi(score);
if (scoreValue >= 80 || isTop3(evaluate)) {
return "perfect" + evaluate.substr(evaluate.find("}") + 1);
} else {
return "better" + evaluate.substr(evaluate.find("}") + 1);
}
}
bool isTop3(string evaluate) {
string sort = "";
for (char c : evaluate) {
if (!isdigit(c)) {
break;
}
sort += c;
}
return stoi(sort) <= 3;
}
int main() {
string line;
while (getline(cin, line)) {
string company = line;
getline(cin, line);
string team = line;
getline(cin, line);
string result = line;
vector<string> results;
stringstream ss(result);
string item;
while (getline(ss, item, ',')) {
results.push_back(item);
}
cout << getResult(company, team, results) << endl;
}
return 0;
}
```
在C++代码中,我们使用了`iostream`和`sstream`库来处理输入和输出。我们使用`getline`函数从标准输入逐行读取输入,并使用`stringstream`来将字符串分割为多个部分。
主要的逻辑函数 `getResult`,`parseScore` 和 `convertResult` 在C++中与Java中的实现几乎相同。
在主函数中,我们使用 `getline(cin, line)` 逐行读取输入,并将每行输入的内容存储在相应的变量中。然后,我们使用 `stringstream` 将结果字符串分割为多个部分,并存储在一个字符串向量中。
最后,我们调用 `getResult` 函数获取结果,并使用 `cout` 打印输出。
请注意,在C++中,字符串的比较使用 `==` 运算符而不是 `!=` 运算符。另外,C++ 中没有 `equalsIgnoreCase` 函数,我们可以直接使用 `==` 进行大小写敏感的比较。同时,我们使用 `stoi` 函数将字符串转换为整数。
这是一个等效的C++实现,但可能还有其他优化和改进的方式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)