std::string 数组元素访问 [英] std::string Array Element Access

查看:54
本文介绍了std::string 数组元素访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管是一个 Project Euler 程序,但以下代码实际上并不关心它.我想添加 50 个 100 位数字,并将每个数字的每个数字分配给数组 addends[100][50] 中的一个元素.然后我将每个数字/位置单独相加,并携带额外的数字.这些数字是从名为 Input.txt 的文本文件中读取的,它仅包含所有数字.http://projecteuler.net/problem=13

Despite being a Project Euler program, the following code doesn't actually concern it much. I want to add 50 100-digit numbers, and I'm assigning each digit of each number to an element in the array addends[100][50]. I'd then add up each digit/place individually, and carry over extra digits. The numbers are being read in from a text file named Input.txt, and it merely contains all the numbers. http://projecteuler.net/problem=13

我无法将字符分配给来自文件输入流 () 的字符串数组 (string numbers[100][50]) 的元素).问题在评论中得到了更完整的描述:

I'm having trouble assigning characters to elements of a string array (string numbers[100][50]) from a file input stream (<fstream>). The problem is described more completely in the comments:

"[对于第一个循环] 这个循环为字符串数组中的每个字符串分配一个数字.即使第二个数字 (50) 没有做任何事情(它似乎被 std::string 覆盖;参见变量声明),它需要在那里才能使循环工作.循环的逻辑"相同;j"什么也不做,但需要在那里才能使循环工作?"

"[for the 1st loop] This loop assigns a number to every string in the string array. Even though the second number (50) doesn't do anything (it seems to be overridden by std::string; see variable declaration), it needs to be there for the loop to work. Same "logic" for the loop; "j" doesn't do anything but needs to be there for the loop to work?"

而且,(对于第二个循环)此循环从相应的字符串数组元素中填充addends[100][50]"数组.如果我尝试使用数组调用char_to_int()"数字[i][j]",编译器抱怨输入的数据类型不正确.添加变量k"使循环运行一次,但最终在第二个循环中崩溃(使用numbers[i][j][k]".所以我尝试了char_to_int((numbers[i][j]).c_str())",但编译器抱怨const char *"与char"不兼容.添加一个指针解决了这个问题(char_to_int( *( (numbers[i][j]).c_str() ) )"),但程序稍后仍然崩溃.我去掉了一些无关紧要的代码,使其更具可读性.

And also, (for the 2nd loop) "This loop fills in the "addends[100][50]" array from the corresponding string array element. If I try to call "char_to_int()" with the array"numbers[i][j]", the compiler complains that the input isn't of the right data type. Adding a variable "k" makes the loop work for one run, but eventually crashes on the second loop (using "numbers[i][j][k]"). So I tried "char_to_int((numbers[i][j]).c_str())", but the compiler complains that "const char *" is incompatible with "char". Adding a pointer resolves the issue ("char_to_int( *( (numbers[i][j]).c_str() ) )"), but the program still crashes later." I took out some code that doesn't matter to make it more readable.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int char_to_int(char chInput);

int main()
{

    int placeholder;    //so console doesn't close immediately upon finish
    int sum[102] = {0}; // 100+2, 100 places + 2 places from carrying over
    int addends[100][50] = {0};
    string numbers[100][50];
    ifstream input("Input.txt");

    /* This loop assigns a number to every string in the string array. Even
     * though the second number (50) doesn't do anything (it seems to be
     * overridden by std::string; see variable declaration), it needs to be
     * there for the loop to work. Same "logic" for the loop; "j" doesn't do
     * anything but needs to be there??? Confused :-\
     */
    for (int i = 0; i < 100; i++)
        for (int j = 0; j < 1; j++)
            getline(input, numbers[i][j]);

    /* This loop fills in the "addends[100][50]" array from the corresponding
     * string array element. If I try to call "char_to_int()" with the array
     * "numbers[i][j]", the compliler complains that the input isn't of the
     * right data type. Adding a variable "k" makes the loop work for one run,
     * but eventually crashes on the second loop (using "numbers[i][j][k]").
     * So I tried "char_to_int((numbers[i][j]).c_str())", but the compiler
     * complains that "const char *" is incompatible with "char". Adding a
     * pointer resolves the issue ("char_to_int( *( (numbers[i][j]).c_str() ) )"),
     * but the program still crashes on the second loop through.
     */
    for (int i = 0; i < 100; i++)
        for (int j = 0; j < 50; j++)
            for (int k = 0; k < 1; k++) //used when the variable "k" was being used
                addends[i][j] = char_to_int( (numbers[i][j]).c_str() );

    return 0;
}

代码未完成;我决定不继续,因为我(显然)需要先解决这个问题.

The code isn't finished; I decided against going on since I (obviously) need to fix this first.

推荐答案

它编译并运行良好

string numbers[100];

for (int i = 0; i < 100; i++)
        getline(input, numbers[i]);

for (int i = 0; i < 100; i++)
    for (int j = 0; j < 50; j++)
            addends[i][j] = char_to_int( (numbers[i][j]));

删除 stdafx.h 包含并定义 char_to_int 后.

after removing the stdafx.h include and defining char_to_int.

std::string 本身包含一个字符数组,因此您只需要一个 std::string 的一维数组.然后您可以通过[] 索引访问字符串的字符,

A std::string contains an array of characters itself, so you only need a one-dimensional array of std::strings. You can then access the characters of a string by [] indexing,

numbers[i][j]

获取数组 numbers 中第 i 个字符串的第 j 个字符(而不是字节).

gets the j-th character (byte, rather) of the i-th string in the array numbers.

这篇关于std::string 数组元素访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆