C++ 一个数组中的 Char 和 Int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8043359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:54:41  来源:igfitidea点击:

Char and Int in one Array

c++carrayscharint

提问by soniccool

I want to have chars and ints inside one array. What i am trying to do is have 1 to 9 in my array and the user selects which number to replace with the letter X. How can i have this done? I assume i cant pass chars into an array that is called as int array[8];So is there a way to have both ints and chars in an array?

我想在一个数组中包含字符和整数。我想要做的是在我的数组中有 1 到 9 并且用户选择用字母 X 替换哪个数字。我怎么能做到这一点?我假设我不能将字符传递到一个名为 as 的数组中int array[8];那么有没有办法在数组中同时包含整数和字符?

回答by James Webster

In c++ ints and chars are almostthe same thing. They are both stored as numbers, just with different resolutions.

在 c++ 中,ints 和chars几乎是一回事。它们都存储为数字,只是分辨率不同。

int array[2];
array[0] = 100;
array[1] = 'c';

printf("%d", array[0]) //Prints the number at index zero.

//Is it %c to print a char?
printf("%c", array[1]) //Prints the number at index zero as it's equivalent char.

回答by Seth Carnegie

Why don't you just use an array of characters?

为什么不直接使用字符数组?

You can do

你可以做

char characters[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 0}; // last one is NULL terminator

int replace = 1;
cout << "Enter the number you want to replace with X: ";
cin >> replace;

assert(replace > 0 && replace < 10); // or otherwise check validity of input

characters[replace - 1] = 'X';

// print the string
cout << characters;

// if the user entered 5, it would print
// 1234X6789

回答by Mranz

A stab in the dark, because there is a large distinction between the user-model and the programming model.

暗中刺伤,因为用户模型和编程模型之间存在很大差异。

When the user 'inserts' a character at a specified index, you want to update an array of char instead of inserting the value in your int array. Maintain the 2 side-by-side.

当用户在指定的索引处“插入”一个字符时,您希望更新一个 char 数组,而不是将值插入到您的 int 数组中。保持 2 并排。

回答by nimrodm

As others have mentioned, charwill be promoted to intwhen you assign elements of the int[]array with charvalues. You would have to use an explicit cast when you are reading from that array.

正如其他人所提到的,当您为数组的元素分配值时,char将被提升为。当您从该数组中读取时,您必须使用显式强制转换。intint[]char

I.e., the following works

即,以下作品

int a[10];
char c='X';

a[0] = c;
c = (char) a[0];

HOWEVER,

然而,

Since you would need to keep track of which elements hold ints and which hold chars -- this is not an attractive solution.

由于您需要跟踪哪些元素包含整数,哪些元素包含字符——这不是一个有吸引力的解决方案。

Another option is just have an array of charand store the digits 0..9 as chars. I.e., '0','1', ..'9'.

另一种选择是只拥有一个数组char并将数字 0..9 存储为字符。即,'0'、'1'、..'9'。

(A third option is just have another variable store the index to the 'X' element -- but this is very different than what you are suggesting)

(第三个选项只是让另一个变量存储“X”元素的索引——但这与你所建议的非常不同)

回答by parapura rajkumar

You can treat your numbers as characters

您可以将您的数字视为字符

    char mychar[10];
    for ( int i = 0; i < 10; ++i )
    {
        mychar[i] = '0' + i;
    }


    //Assume you read it 
    int userInput = 9;
    mychar[userInput-1] = 'X';

回答by Nice Books

The simplest solution is to use -1 instead of X assuming that your array does not have any negative numbers. I have done that before.

最简单的解决方案是使用 -1 而不是 X 假设您的数组没有任何负数。我以前这样做过。