仅使用存储在数组中的字符串的第一个字母
Using Only the First Letter in a String Stored in an Array
我目前正在为MC68HC11编写一个"伪汇编编译器",这并不复杂。我遇到的问题是从文件读取并存储到数组后。
例如,我有一行"LDAA #$45",我首先将"LDAA"保存到字符串数组中,并将"#$45"保存到第二个字符串数组中。我按原样使用第一个数组,但对于第二个数组,我只需要知道该数组中的第一个字母或符号是什么,这样我就可以知道我需要在哪个if语句中结束。
进入LDAA的代码是这样的:
if(code[i]=="LDAA"){ //code is my array for the first word read.
if(number[i]=="#"){ //Here's where I would only need to read the first symbol stored in the array.
opcode[i]="86";
}
}
我使用的代码从文件读取类似于读取文件到数组?
我不确定这是否完全可能,因为我在网上找不到类似的东西。
根据number
的类型,您需要:
if(number[i]=='#'){
或
if( number[i][0]=='#'){
code[i]
、opcode[i]
为std::string
或char*
型。[希望是前者]
您已经标记了这个c++,所以我将假设您的数组包含std::string
s,在这种情况下:
#include <string>
#include <iostream>
int main()
{
std::string foo = "#$45";
std::string firstLetter = foo.substr(0, 1);
std::cout << firstLetter;
return 0;
}
产生的输出:
#
这是你想要的吗?应用于您的代码:
if(code[i]=="LDAA"){
if(number[i].substr(0, 1)=="#"){
opcode[i]="86";
}
}
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何仅读取文本文件中的第一个值
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++:忽略第一个 cin.ignore 之后的输入
- C++:递归地将字符串中一个字母的所有实例替换为另一个字母
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- OpenGL:第二个VBO破坏了第一个VBO
- 为什么第一个Dynamic_cast没有投射到基类?
- OpenGL 2D游戏只绘制第二个精灵纹理而不是第一个
- C++ 为什么程序只读取第一个值
- 使用C 中字符串标头的getline()函数将第一个char存储在字符串对象中
- 在将字符串转换为整数后,是否有一种方法可以保持数字的第一个零
- 将第一个boost::regex匹配放入字符串中
- 有一个字符串如何从第一个' /r/n/r/n '得到它的内容,直到2行从字符串结束
- 删除二进制字符串中的第一个0