简体   繁体   English

来自外部文件问题的C ++多维数组

[英]C++ Multi dimensional array from external file question

I'm trying to get a contiguous line with values separated by "&" to load into a multi-dimensional array. 我正在尝试获得一条连续的行,其值用“&”分隔,以加载到多维数组中。 Here's the way I'm trying to do it - Everything checks out in the code, except the string "str" which contains my separated values in the format "value1, value2, value3, etc..." just loads that whole string into array[0][0]. 这就是我尝试的方式-一切都在代码中检出,除了字符串“ str”包含我的单独值(格式为“ value1,value2,value3等...”)只是将整个字符串加载到数组[0] [0]。 I know there are better ways of doing this, but what I would like to know is why C++ won't treat "str" as if I had typed out the individual values and hard coded "array". 我知道有更好的方法可以做到这一点,但是我想知道的是为什么C ++不会像对待我键入单个值和硬编码“数组”那样对待“ str”。

Here is the code: 这是代码:

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

using namespace std;


int main(int argc, char* argv[])
{

    string str, strTotal;

    ifstream in;

    in.open("Desktop/01_001.PAC");

    getline(in,str);
    while ( in ) {
        strTotal += str;
        getline(in,str);
    }


    string searchString( "&" ); 
    string replaceString( ", " );

    assert( searchString != replaceString );

    string::size_type pos = 0;
    while ( (pos = str.find(searchString, pos)) != string::npos ) {
        str.replace( pos, searchString.size(), replaceString );
        pos++;
    }



    string array[4][5] = {str};

    cout << array[0][0];

    return(0);
}

And here is the external file ( "Desktop/01_001.PAC" ): 这是外部文件( "Desktop/01_001.PAC" ):

void&void&void&void&a&a1&a2&a3&b&b1&b2&b3&c&c1&c2&c3&d&d1&d2&d3

Thanks in advance! 提前致谢!

If I'm reading your code correctly, you appear to be searching through the string (loaded from file), and only assigning the very last result to an array index (x=4, y=5). 如果我正确地阅读了您的代码,则您似乎正在搜索字符串(从文件加载),并且仅将最后一个结果分配给数组索引(x = 4,y = 5)。 So your code is doing something like this: 因此,您的代码正在执行以下操作:

while (have not found last variable)
    search for next variable in string

assign variable to (4,5) in matrix

So that last assignment might even work, but since you only assign at the end, the array is not going to be filled the way I think you want it to be filled. 这样最后一次分配甚至可以工作,但是由于您只在末尾分配,因此数组将不会按照我认为您希望其被填充的方式进行填充。

I'm going to assume the matrix you want is always the same size, otherwise things get more complicated. 我将假设您想要的矩阵总是相同的大小,否则情况会变得更加复杂。 In this case, you could use something like this: 在这种情况下,您可以使用以下方式:

let xMax = 4
let yMax = 5

for (x from 0 to xMax)
    for (y from 0 to yMax)
        find the next variable in the string
        assign it to the current (x,y) location in matrix

Debug statements are your friend here! 调试语句是您的朋友! Try the above solution without saving it to an array, and instead print out each term, to see if it is working correctly. 尝试上述解决方案而不将其保存到数组中,而是打印出每一项,以查看其是否正常运行。

I would also point out that the string "void" is not the C++ keyword void, and so will not work if you want an array index to be void. 我还要指出,字符串“ void”不是C ++关键字void,因此如果您希望数组索引为void则将不起作用。 Try getting your code to work without voids at first. 首先尝试使代码正常运行。

Because code and data are different things. 因为代码和数据是不同的东西。 Your code is compiled before it runs. 您的代码在运行之前已被编译。

It sounds as if this is what you expect: 听起来这就是您所期望的:

  1. The string contains the text "foo, bar, baz". 该字符串包含文本“ foo,bar,baz”。

  2. The statement string[] whatever = {str}; 语句string[] whatever = {str}; is run. 运行。

  3. Since "str" contains "foo, bar, baz", you want it to have the same effect as if the line of code were actually string[] whatever = {"foo", "bar", "baz"} . 因为“ str”包含“ foo,bar,baz”,所以您希望它具有与代码行实际上是 string[] whatever = {"foo", "bar", "baz"}

Asking something like this implies a complete misunderstanding of how programming works. 提出这样的问题意味着对编程工作原理的完全误解。

Nothing this magical will ever happen in C++. C ++永远不会发生这种神奇的事情。 It cannot , because (a) what if you actually wanted to put 'str' into the array? 不能 ,因为(a)如果您实际上想将'str'放入数组怎么办? (b) what if 'foo', 'bar' and 'baz' were also variables in your program - should they be interpreted the same way? (b)如果在程序中'foo','bar'和'baz'也是变量,应该怎么解释?

Variable names are not text. 变量名称不是文本。 They no longer exist, for all practical purposes, at the time that your code runs . 出于所有实际目的,它们在代码运行时不再存在 They are only there so that you, as the programmer, can say "the value that is used over here should be the same one that is used over there ". 它们只是在那里,以便您作为程序员可以说“ 这里使用的值应该与那里使用的值相同”。

Further, array initializations in C++ do not care how many elements are actually in the initialization vs. the declared size of the array . 此外,C ++中的数组初始化并不关心初始化中实际有多少个元素,而不管数组的声明大小 Any additional elements will be default-initialized (ie, assigned empty strings). 任何其他元素将被默认初始化(即,分配空字符串)。

A string cannot be treated like an array of strings, because it isn't one . 字符串不能视为字符串数组,因为它不是一个 If you want an array of strings, then build it, using the individual string elements as you determine them. 如果需要字符串数组,请在确定字符串时使用各个字符串元素来构建它们。

But since you don't know in advance how many elements there are, you should use std::vector instead of an array. 但是由于您事先不知道有多少个元素,因此应该使用std::vector而不是数组。 And why are you trying to arrange the data into a 2-dimensional structure? 为什么要尝试将数据排列为二维结构? How are you expecting to know how "wide" it should be? 您如何期望知道应该有多“宽”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM