C++ 如何从字符串转换为字符*
我有这个代码,我需要输入一些数据,然后程序需要按字母顺序排列数据 问题是我无法将C++ 如何从字符串转换为字符*,c++,string,pointers,char,alphabetical,C++,String,Pointers,Char,Alphabetical,我有这个代码,我需要输入一些数据,然后程序需要按字母顺序排列数据 问题是我无法将字符串名转换为char*变量s1和s2 要输入的数据: 1 Cheile_Dobrogei 45 25 200 2 Vulcanii_Noroiosi 46 25 50 3塞塔蒂厄伊斯特拉45 25 100 4巴拉朱尔·西里乌51 30 50 5卡斯特罗贝利45 30 150 6 Castellu_Bran 53 30 150 7 Voronet 54 35 200 8 Cheile_Bicazului 55 35
字符串名
转换为char*
变量s1
和s2
要输入的数据:
1 Cheile_Dobrogei 45 25 200
2 Vulcanii_Noroiosi 46 25 50
3塞塔蒂厄伊斯特拉45 25 100
4巴拉朱尔·西里乌51 30 50
5卡斯特罗贝利45 30 150
6 Castellu_Bran 53 30 150
7 Voronet 54 35 200
8 Cheile_Bicazului 55 35 100
9 Manastirea_Varatec 56 35 50
#包括
#包括
使用名称空间std;
结构目标{
int-id;
字符串名;
双纬度;
双纵;
双重成本;
};
int main()
{
内部i、k、温度;
结构目标ob[9];
cout ob[i].id>>ob[i].name>>ob[i].latitud>>ob[i].longitud>>ob[i].cost_vizitare;
}
结构目标tempob[9];
结构目标t[9];
对于(i=0;i<9;i++){
tempob[i]=ob[i];
}
整数排序;
对于(k=0;k<9;k++){
排序=1;
对于(i=0;i<9;i++){
char*s1=tempob[i]。名称;
char*s2=tempob[i+1]。名称;
如果(strcmp(s1,s2)>0){
t[i]=ob[i];
tempob[i]=tempob[i+1];
tempob[i+1]=t[i];
排序=0;
}
}
如果(排序==1){
打破
}
}
cout在代码中甚至不必使用C字符串。换3行
char* s1 = ...;
char* s2 = ...;
到
及
到
您可以保留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';
}
}
#包括
#包括
#包括
结构目标{
int-id;
std::字符串名;
双纬度;
双纵;
双重成本;
};
int main()
{
std::阵列ob;
std::cout ob[i].id>>ob[i].name>>ob[i].latitud>>ob[i].longitud>>ob[i].cost\u vizitare;
}
自动tempob=ob;
对于(int k=0;k<9;k++){
布尔排序=真;
对于(int i=0;i<9;i++){
常数auto&s1=tempob[i]。名称;
常量auto&s2=tempob[i+1]。名称;
如果(s1>s2){
标准:交换(tempob[i],tempob[i+1]);
排序=假;
}
}
如果(已排序){
打破
}
}
std::cout可以通过多种不同的方式实现(从字符串转换为字符*):
1.使用const_cast运算符:
std::string str = "from string to char*";
char *chr = const_cast<char*>(str.c_str());
std::cout << chr << "\n";
std::string str=“从字符串到字符*”;
char*chr=const_cast(str.c_str());
std::coutchar*s1=tempob[i].name.c_str()
@JerryJeremiahc_str()
返回一个const char*
,该字符不能分配给char*
。因此,要么s1
和s2
需要声明为const char*
(首选),要么c_str()
返回的指针需要使用const_cast
类型转换为char*
(不太首选)。为什么还要使用c字符串?使用字符串conststd::string&s1=代码>,常量std::string&s2=编码>并将if(strcmp(s1,s2)>0)
替换为if(s1>s2){
那么为什么需要char*
而不是const char*
?为什么不使用内置于std::string
?@RemyLebeau实际上您只需要char*s1=&tempob[i].name[0]
如果你真的想要char*
。不需要常量
谢谢!我按照你说的做了,它正在工作。(+对代码做了一些小的更改,因为循环返回了代码-416494179(随机数)加上0)@SergiuLuca“一些小的更改”???代码对我有效:它现在有效,就像你做的一样。idk发生的事情o_oI会把s1
和s2
全部去掉:if(tempob[i].name>tempob[i+1].name){…}
if (s1 > s2) {
#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';
}
}
std::string str = "from string to char*";
char *chr = const_cast<char*>(str.c_str());
std::cout << chr << "\n";
std::string str = "from string to char*";
char *chr = strcpy(new char[str.length() + 1],str.c_str());
std::cout << chr << "\n";
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!
std::string str = "from string to char*";
char *chr = &*str.begin();
std::cout << chr << "\n";