使用 CIN 更改二维字符串数组值
using cin to change a 2d string array value
const string astrDice[6][4]
{
{"M1" , "DM" , "AT" , "BK"},
{"M2" , "DM" , "BK" , "BK"},
{"M3" , "DM" , "BK" , "BK"},
{"M4" , "BK" , "AT" , "BK"},
{"M5" , "BK" , "AT" , "BK"},
{"M6" , "BK" , "AT" , "BN"}
};
cout << "Choose Side (1-6):";
int nSideChoice = 0;
cin >> nSideChoice; // will -1 later for ease of use to operator
cout << "Choose a Slot (1-4):";
int nSlotChoice = 0;
cin >> nSlotChoice; // will -1 later for ease of use to operator
cout << "Enter a New Value:";
string strNewSlot;
cin >> strNewSlot;
astrDice[nSideChoice - 1][nSlotChoice - 1] = strNewSlot;
在最后一行说"没有可行的重载"="。我想使用 nSideChoice 和 nSlotChoice 来更改数组中的选择。我还在学习如何做基本的代码,现在只知道一些C++。这只是我一直在做的一个测试程序,随着我学习新事物而不断变化。
看这一行: const string astrDice[6][4]
星体骰子是常量,你不能修改它!
编译器应该说类似 error: passing 'const string' as 'this' argument of 'something' discards qualifier
.当您看到这样的消息时,通常意味着您有恒常性问题。
No Viable Overloaded "="
不是有史以来最好的错误消息。它试图说 std::string 没有 const 运算符=()。嗯,嘟嘟。
尝试理解错误消息很重要,即使它们有时有点愚蠢。您将节省大量时间。
你的数组是const
的,这意味着你不能在初始化后修改它。删除const
限定符,您的代码应该可以工作。
由于此处的限定符const
const string astrDice[6][4]
您将无法编辑此数组。是的,您可以将元素更改为string astrDice[6][4]
但这真的是一个很好的解决方案吗?
让我们在这一行看一行:
cout << "Choose Side (1-6):";
int nSideChoice = 0;
cin >> nSideChoice;
如果用户将侧面选择为 7 怎么办?然后在这里:astrDice[nSideChoice][nSlotChoice] = strNewSlot;
您将遇到严重的内存问题。请考虑为此使用std::vector
:
vector<vector<string>> astrDice =
{
{"M1" , "DM" , "AT" , "BK"},
{"M2" , "DM" , "BK" , "BK"},
{"M3" , "DM" , "BK" , "BK"},
{"M4" , "BK" , "AT" , "BK"},
{"M5" , "BK" , "AT" , "BK"},
{"M6" , "BK" , "AT" , "BN"}
};
cout << "Choose Side (1-6):";
int nSideChoice = 0;
cin >> nSideChoice;
cout << "Choose a Slot (1-4):";
int nSlotChoice = 0;
cin >> nSlotChoice;
cout << "Enter a New Value:";
string strNewSlot;
cin >> strNewSlot;
astrDice.at(nSideChoice).at(nSlotChoice) = strNewSlot;
这样,您将免受越界数组问题的影响。
O_o哇......事实上
定义约束以编写程序或主体函数...
-> 您要求用户输入数值(架构位的符号值:示例32位)-> 这不是您对代码的要求,这些代码需要介于 1-6 和以后的 1-4 之间的范围值-> 你自己计算数组的矩阵维数(是矩阵还是向量的向量)--->向量的向量不对每个向量的长度添加约束,矩阵添加它--->当您不确定时,请使用切片器,简单性胜过复杂性->键地址应由地图容器而不是矢量处理
你使用什么语言并不重要,只要在编码开始时就具体并处理约束即可。
此外,STD::CIN 是错误 PRONE ...这是一种非常基本的低级读取值机制(窥视,获取,读取和忽略在很多方面对菜鸟都是有害的),其中几乎99%的人想要一个正则表达式控件进行输入。
#include <iostream>
#include <regex>
#include <string>
#include <map>
#include <valarray>
using namespace std;
const int IHM_MAXREAD_LOOP= 3;
const int BLCK_SIDE= 6;
int main()
{
valarray<string> astrDice= // deserialized matrix
{"M1" , "DM" , "AT" , "BK"
,"M2" , "DM" , "BK" , "BK"
,"M3" , "DM" , "BK" , "BK"
,"M4" , "BK" , "AT" , "BK"
,"M5" , "BK" , "AT" , "BK"
,"M6" , "BK" , "AT" , "BN"
};
map<int,valarray<string> > astrDice;
for(int i=0;i<BLCK_SIDE;++i) astrDice[i]=data[slice(i, BLCK_SIDE, 1)]; // adding constraint on block size , remove constraint on index
string in1,in2, readyparse;
smatch match;
regex re("[1-6] [1-4]");
int maxtry;
for( maxtry = IHM_MAXREAD_LOOP; !regex_match(readyparse, match, re) && maxtry>0 ; maxtry--)
{
cout << "Choose Side (1-6):";
cin >> in1;
cout << "Choose a Slot (1-4):";
cin >> in2;
readyparse = in1+" "+in2;
}
if(maxtry<=0)
{
cout << "Run only with 1 2 3 4 5 or 6 for Side and 1 2 3 or 4 for Slot " << std::endl;
}else {
cout << "Enter a New Value:";
cin >> astrDice[atol(in1.c_str())-1][atol(in2.c_str())-1];
}
}
- 如何将整数字符串转换为整数的二维向量?
- 如何在Visual Studio C++中声明一个二维字符串数组?
- 有效地从文本文件中读取带有字符串索引的大型二维数组(矩阵)
- 如何将二维数组类型字符(字符串)作为函数参数传递?
- 我无法将 Excel 中的输入字符串存储为二维字符串
- 在C++中通过引用传递字符串的二维动态数组
- 如何在C++中将字符串的动态二维数组作为参数传递
- C++基于用户输入字符串长度创建二维数组
- C / C 将字符串放入二维数组中
- 将字符串放入目标C++中的二维矩阵中
- 从二维数组中的char*初始化c++字符串时,读取大小为1的无效值
- 增加二维字符串向量中外部向量的大小
- 使用 CIN 更改二维字符串数组值
- 如何正确显示二维字符串数组
- Push_back字符串在向量中的位置到二维向量
- 将字符串转换为二维数组
- C++二维数组,包含字符串和int
- C++将二维C字符串元素传递到函数中
- 将二维字符串数组与std::sort进行比较
- 输出二维字符串数组时出现segfault