2013-01-01 61 views
-3

我有一个字符串数组,它保留从文件读取的数据。它由76条线组成。将数组分成多个数组C++

我想要做的是将它们存储在不同的数组中。 类似于从第21行到第31行的1个阵列。第31到第41个阵列。我怎么办... plz帮助

我想将70行分成7个数组,每个数组包含10行。并做到这一点,而无需使用矢量

+5

[Not this again。](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – chris

+0

7 * 10!= 77 ..你想做吗 ? – asheeshr

+0

你有没有考虑过使用'std :: vector >>(或'std :: map >>')作为你的容器?要么会让你的生活更轻松。并按照@chris留给你的链接,它会为你节省一个痛苦的世界。 – Johnsyweb

回答

2

您使用等于运算==并没有工作。在整个循环执行中,i将仅等于6次中的一个值。只有当i1122,33,44,5566;对于任何其他值i您的循环将做没有

您可能改为使用<

+0

我想知道为什么你删除了该评论。看起来我应该注意。 – chris

1

事情是这样的:

getline(ol, arr[i/11][i%11]); 

其中ARR是std::vector<std::string>std::vector。或者是数组字符串的数组。

还有另一种方法是:

while (1) { 
    std::string *ptr; 
    if (i < 11) ptr = arr1; 
    else if (i < 22) ptr = arr2; 
    // long list of arrays 

    getline(ol, ptr[i%11]); 
    // increment i, break on eof... 
} 
+1

由于某些未知原因,载体已经不存在。 – chris

+0

我解决了我的自我。首先在一个循环中初始化两个变量, 例如:for(int i = start; j = 0; i

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

using namespace std; 

int main() 
{ 
    string arr[8][10]; 

    int i = 0, j = 0; 

    ifstream ol("a.txt"); 

    while(getline(ol, arr[i][j])) 
    { 

     ++j; 
     if(j == 10) 
     { 
      ++i; 
      j = 0; 
     } 
    } 

} 

这是假设你没有超过80行。