C++ 如何在c++;?
我有以下字符数组,它们对应于前面给出的整数C++ 如何在c++;?,c++,arrays,C++,Arrays,我有以下字符数组,它们对应于前面给出的整数 char a[100] = "hi", //---corresponds to integer 1. b[100] = "my", // 2. c[100] = "name", // 3. d[100] = "is", // 4. e[100]
char a[100] = "hi", //---corresponds to integer 1.
b[100] = "my", // 2.
c[100] = "name", // 3.
d[100] = "is", // 4.
e[100] = "nis", // 5.
f[100] = "hu"; // 6.
int x,y;
cin >> x >> y;
假设每个数组中都有一些内容
现在,我将两个整数x和y(介于1到6之间)作为输入,并基于x和y交换相应的数组
我不想使用字符串类型的数组。而且我认为我可以使用enum来实现这一点,只是我似乎不明白怎么做
样本输入1 3
示例输出a=name c=hi您可以使用以下命令:()
#包括
#包括
bool-isValidIndice(int-index){返回1x>>y;
if(isValidIndice(x)和&isValidIndice(y)){
std::cout最后使用字符串类型的数组完成它
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
int x,y;
cout<<"Exchange String\n\n";
string a[6];
a[0]="hi";a[1]="my";a[2]="name";a[3]="is";a[4]="nis";a[5]="hu";
while(1){
cout<<"1) "<<a[0]<<"\n2) "<<a[1]<<"\n3) "<<a[2]<<"\n4) "<<a[3]<<"\n5) "<<a[4]<<"\n6) "<<a[5];
cout<<"\nChoose x and y in between 1 to 6, type -1 to quit" ;
cin>>x >> y;
if(x==-1){
exit(1);
}
string temp = a[x-1];
a[x-1] = a[y-1];
a[y-1]= temp;
}
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
int x,y;
如果有很多数组,您可以使用std映射将整数与指向这些数组的指针相关联,然后根据需要进行交换(位集+xor?仅使用带临时值的普通内存?您可以选择)。最佳解决方案是使用二维数组,arr[6][100]<或代码>或双指针<代码> char **/COD>然后交换。如何建立对应关系?“交换数组意味着什么?”DeDe复Cal:我没有建立对应关系,如果X是1,Y是3,C的内容和C的内容,我想。因为你问C++,不只是C,我想你会想要的。使用std::vector,或者可能使用std::set或std::map,而不是C数组。但所问的问题有点令人困惑。能否发布数组初始内容的示例以及所需的结果?请注意,您可以使用a[x-1].swap(a[y-1])
来交换内容,而不是手动进行交换。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
int x,y;
cout<<"Exchange String\n\n";
string a[6];
a[0]="hi";a[1]="my";a[2]="name";a[3]="is";a[4]="nis";a[5]="hu";
while(1){
cout<<"1) "<<a[0]<<"\n2) "<<a[1]<<"\n3) "<<a[2]<<"\n4) "<<a[3]<<"\n5) "<<a[4]<<"\n6) "<<a[5];
cout<<"\nChoose x and y in between 1 to 6, type -1 to quit" ;
cin>>x >> y;
if(x==-1){
exit(1);
}
string temp = a[x-1];
a[x-1] = a[y-1];
a[y-1]= temp;
}
}