[英]How to convert from string to char*
I have this code, in which I need to input some data, and then the program needs to put the data in alphabetical order.我有这段代码,我需要在其中输入一些数据,然后程序需要按字母顺序放置数据。
The problem is that I can't convert string name
to the char*
variables s1
and s2
.问题是我无法将string name
转换为char*
变量s1
和s2
。
Data to input:要输入的数据:
1 Cheile_Dobrogei 45 25 200
2 Vulcanii_Noroiosi 46 25 50
3 Cetatea_Istra 45 25 100
4 Barajul_Siriu 51 30 50
5 Castelul_Peles 45 30 150
6 Castelul_Bran 53 30 150
7 Voronet 54 35 200
8 Cheile_Bicazului 55 35 100
9 Manastirea_Varatec 56 35 50
#include <iostream>
#include <string>
using namespace std;
struct obiectiv {
int id;
string name;
double latitud;
double longitud;
double cost_vizitare;
};
int main()
{
int i, k, temp;
struct obiectiv ob[9];
cout << "Introduceti obiectivele(maxim 9): ID NAME LATITUD LONGITUD PRICE" << endl;
for (i = 0; i < 9; i++) {
cin >> ob[i].id >> ob[i].name >> ob[i].latitud >> ob[i].longitud >> ob[i].cost_vizitare;
}
struct obiectiv tempob[9];
struct obiectiv t[9];
for (i = 0;i < 9;i++) {
tempob[i] = ob[i];
}
int sorted;
for (k = 0; k < 9;k++) {
sorted = 1;
for (i = 0;i < 9;i++) {
char* s1 = tempob[i].name;
char* s2 = tempob[i + 1].name;
if (strcmp(s1,s2) > 0) {
t[i] = ob[i];
tempob[i] = tempob[i + 1];
tempob[i + 1] = t[i];
sorted = 0;
}
}
if (sorted == 1) {
break;
}
}
cout << "alphabetical order: ";
for (i = 0; i < 9; i++) {
cout << tempob[i].name << endl;
}
}
It's not necessary to even use C-strings in your code.甚至没有必要在代码中使用 C 字符串。 Change 3 lines更改 3 行
char* s1 = ...;
char* s2 = ...;
to至
const std::string &s1 = ...;
const std::string &s2 = ...;
and和
if (strcmp(s1,s2) > 0)
to至
if (s1 > s2) {
and you can keep std::string
:你可以保留std::string
:
#include <array>
#include <iostream>
#include <string>
struct obiectiv {
int id;
std::string name;
double latitud;
double longitud;
double cost_vizitare;
};
int main()
{
std::array<obiectiv, 9> ob;
std::cout << "Introduceti obiectivele(maxim 9): ID NAME LATITUD LONGITUD PRICE" << '\n';
for (int i = 0; i < 9; i++) {
std::cin >> ob[i].id >> ob[i].name >> ob[i].latitud >> ob[i].longitud >> ob[i].cost_vizitare;
}
auto tempob = ob;
for (int k = 0; k < 9;k++) {
bool sorted = true;
for (int i = 0; i < 9; i++) {
const auto &s1 = tempob[i].name;
const auto &s2 = tempob[i + 1].name;
if (s1 > s2) {
std::swap(tempob[i], tempob[i + 1]);
sorted = false;
}
}
if (sorted) {
break;
}
}
std::cout << "alphabetical order: ";
for (int i = 0; i < 9; i++) {
std::cout << tempob[i].name << '\n';
}
}
There was an error in the swap logic.交换逻辑中存在错误。 I replaced it with std::swap
to fix the error.我用std::swap
替换它来修复错误。
Using std::array
instead of C-arrays allows you to copy an array without loop.使用std::array
而不是 C-arrays 允许您复制没有循环的数组。
I also changed some bad styles in the code.我还在代码中更改了一些不好的 styles。 I removed using namespace std;
我删除了using namespace std;
, struct
in front of a struct
initialization, unnecessary std::endl
and I made sorted
a boolean. , struct
前面有一个struct
初始化,没必要std::endl
和我做了个boolean sorted
。
It's possible to do this(convert from string to char*) in many different ways:可以通过许多不同的方式执行此操作(从字符串转换为 char*):
1.Use const_cast operator: 1.使用 const_cast 运算符:
std::string str = "from string to char*";
char *chr = const_cast<char*>(str.c_str());
std::cout << chr << "\n";
2.Use strcpy(): 2.使用strcpy():
std::string str = "from string to char*";
char *chr = strcpy(new char[str.length() + 1],str.c_str());
std::cout << chr << "\n";
3.Use copy(): 3.使用复制():
std::string str = "from string to char*";
int length = str.size();
char *chr = new char[length + 1];
std::copy(str.begin(),str.end(),chr);
chr[length] = "\0";//add end of line
std::cout << chr << "\n";
delete[] chr; //don't forget!
4.Use contigous storage of std::string: 4.使用std::string的连续存储:
std::string str = "from string to char*";
char *chr = &*str.begin();
std::cout << chr << "\n";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.