还是string和char

#include<iostream>
#include<cstring>
using namespace std;
const string qualify[4] =
{
"10,000-meter race.\n",
"mud tug-of-war.\n",
"master canoe jousting.\n",
"pie-throwing festival.\n",
};
int main()
{

int age;
cout << "Enter your age in years: ";
cin >> age;
int index;
if (age > 17 && age < 35)
index = 0;
else if (age >= 35 && age < 50)
index = 1;
else  if (age >= 50 && age < 60)
index = 2;
else
index = 3;
cout << "You qualify for the " << qualify[index];
return 0;

}





#include<iostream>
const char*qualify[4] =
{
"10,000-meter race.\n",
"mud tug-of-war.\n",
"master canoe jousting.\n",
"pie-throwing festival.\n",
};
int main()
{
using namespace std;
int age;
cout << "Enter your age in years: ";
cin >> age;
int index;
if (age > 17 && age < 35)
index = 0;
else if (age >= 35 && age < 50)
index = 1;
else  if (age >= 50 && age < 60)
index = 2;
else
index = 3;
cout << "You qualify for the " << qualify[index];
return 0;
}

下面的有char型指针的方式运用数组字符串和&&选择范围是重要知识点,上面用了string型的数组虽然可以存放,但不能使用。

引用中提到,可以将一个char数组转为string。具体的方法是将char数组赋值给一个string变量。例如,char数组ch[20]中存储了字符串"abcdef",可以通过str=ch;将其转为string类型的str变量。 引用中提到,同样可以将char指针转为string类型。具体的方法是将char指针赋值给一个string变量。例如,char* a指向字符串"abcdef",可以通过b=a;或者b = string(a);将其转为string类型的b变量。 引用中提到,可以将一个char指针转为char数组。具体的方法是使用strcpy函数将char指针复制给一个char数组。例如,char* s指向字符串"abcdef",可以通过strcpy(ch, s);将其转为char数组ch。 所以,通过上述方法,可以在stringchar之间进行转换。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [stringchar 常用操作](https://blog.csdn.net/yt1242228309/article/details/124853597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [【stringchar】C++ 中stringchar](https://blog.csdn.net/qq_42127861/article/details/120230040)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值