C ++中的字符串数组 [英] arrays of strings in C++

查看:157
本文介绍了C ++中的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在我的C ++代码中声明一个字符串数组.但是我没有得到正确的结果.

我的要求是从csv文件读取,然后将每个字段存储在数组中.

csv文件看起来像这样:


sg,vsvs,22,53,dd ,,, dg
34,2,f,d,343,gg,45,,g,4
gdg ,, 3,fd,gd,3453,53 ,,, 453,STRING1 ,,
aa ,, sg,2,32fwef,3f3f3,f3,STRING1 ,,,,



空字段也在那里,不应忽略.这些空字段也应该被推入字符串数组.

我的上述要求的代码.是:

 #include   <   iostream  > 
 #include   <   fstream  > 
 #include   <  字符串 > 
 #include   <   stdio.h  > 
 #include   <   stdlib.h  > 



使用 命名空间 std;

 int  main()
{
  字符 * arr [ 50 ] [ 50  ];
   const   int  N =  100 ;
  字符 * toks = NULL;
  字符行[ 100 ];
   int  i =  0 ,j =  0  ;
  静态  int  row =  0 ,col =  0 ;
  ifstream myfile(" );

  同时(myfile)
  {
    myfile.getline(line,N);
    toks = strtok(line," );
    同时(toks!= NULL)
    {
      arr [i] [j] = toks;
      toks = strtok(NULL," );
      j ++;
      col = j;
    }
    i ++;
    row = i-  1 ;
    j =  0 ;
  }

   for ( int  i =  0 ; i< ; row; i ++)
  {
     for (j =  0 ; j< col; j ++)
    {
       cout<< arr [i] [j]<< " ;
    }
    cout<< " ;
  }
  myfile.close();

  返回  0 ;
} 




但是结果是:


,sg fwef f 3




我不知道y之间没有很多空格,也无法达到预期的效果.

请任何人帮助我...

谢谢
Deepak

解决方案

记住,您的变量toks被声明为指向char的指针.
因此,如果您要这样做:

 arr [i] [j] = toks; 


您不是将标记指向的内容复制到数组中,而是复制指针本身.
表示此操作后,arr [i] [j]指向与toks相同的内存.

如果您确实要复制char *指向的内容,则可以使用 strcpy() [ ^ ]

在执行此操作时,请考虑以下说明:

  while (toks!= NULL)
{
     // 之后,元素arr [i] [j]指向与toks相同的内存
     arr [i] [j] = toks;
     // 您正在将下一个令牌写入toks指向的内存中(与arr [i]相同[j]!)
     // 在下一次迭代中,toks仍指向同一内存.
     // 您在arr [i] [j]中的所有元素以及toks都指向同一内存!
     toks = strtok(NULL," );
     j ++;
     col = j;
} 



您所有的数组元素都指向您在每次调用strtok时都将覆盖的同一内存.

另一件事是.如果您在C ++环境中,为什么不使用更安全的std :: string和std :: vector?


您的代码当前仅复制指向您所用令牌的指针通过strtok.这些标记存储在输入字符串的静态副本中,该副本存储在line中.这意味着每次您在新行的开头对strtok进行新调用时,此静态副本都将被新行覆盖,因此您以前的指针将全部无效:它们仍指向有效内存,但是里面的内容不一样!

因此,很可能只要您只查看单行输入文件,您的代码就可以正常工作.但是当您开始阅读第二行并将strtok应用于它的那一刻,您的程序就会中断.

正如其他人指出的那样,使用std :: string和std :: vector这样更明智:

std::vector<std::vector<std::string> > arr; // dynamic 2D array of dynamic strings

为了快速修复,只需从声明中替换char*即可:请注意,如果您有超过50行或每行超过50个字符串,则程序将中断!

像这样更改类型将导致将

arr[i][j] = toks;

的含义从仅复制指针更改为实际复制toks指向的字符串.这应该足以解决您的问题.


基本上,您在处理C-like字符串时表现不佳,将临时字符指针插入到数组中.
既然您正在使用C++,为什么不利用其标准库呢?
尝试例如:

 #include   <   iostream  > 
 #include   <  向量 > 
 #include   <   fstream  > 
 #include   <  字符串 > 
 使用 命名空间 std;

 int  main()
{
  ifstream myfile(" );

  向量<向量<字符串> > ar;
  同时()
  {
    字符串s;
     size_t  lastpos,curpos;

    getline(myfile,s);
    如果(!myfile) break ;
    ar.push_back(vector< string>());
     for (lastpos = curpos =  0 ; curpos!= string :: npos; lastpos = curpos + 1)
    {
      curpos = s.find(' ,',lastpos);
      如果(curpos!= string :: npos)
        ar.back().push_back(s.substr(lastpos,curpos-lastpos));
      其他
        ar.back().push_back(s.substr(lastpos));
    }
  }

  myfile.close();

   for ( size_t  i =  0 ; i < ar.size(); i ++)
  {
     for ( size_t  j =  0 ; j < ar [i] .size(); j ++)
    {
        cout<< ar [i] [j]<< " ;
    }
    cout<<恩德尔
  }
  返回  0 ;
} 


Hi ,

I want to declare an array of strings in my C++ code. But i am not getting the correct result.

My req. is to read from a csv file and then store each field in an array.

The csv file looks somehow like this:


sg,vsvs,22,53,dd,,,,dg
34,2,f,d,343,gg,45,,g,4
gdg,,3,fd,gd,3453,53,,,453,STRING1,,
aa,,sg,2,32fwef,3f3f3,f3,STRING1,,,,



Null fields are also there and should not be ignored. These null fields should also be pushed into the string array.

My code for the above req. is :

#include<iostream>
#include<fstream>
#include<string>
#include<stdio.h>
#include<stdlib.h>



using namespace std;

int main()
{
  char* arr[50][50];
  const int N=100;
  char* toks=NULL;
  char line[100];
  int i=0,j=0;
  static int row=0,col=0;
  ifstream myfile("sample.csv");

  while(myfile)
  {
    myfile.getline(line,N);
    toks=strtok(line,",");
    while(toks!=NULL)
    {
      arr[i][j]=toks;
      toks=strtok(NULL,",");
      j++;
      col=j;
    }
    i++;
    row=i-1;
    j=0;
  }

  for(int i=0;i<row;i++)
  {
    for(j=0;j<col;j++)
    {
       cout<<arr[i][j]<<"\t";
    }
    cout<<"\n"; 
  }
  myfile.close();

  return 0;
}




But the result comes as :


,sg fwef f 3




I dont know y there are lots of spaces in between and also the desired result doesn''t come.

Please anyone help me on this...

Thanks
Deepak

解决方案

Remember that your variable toks was declared as a pointer to char.
So if you''re doing this:

arr[i][j]=toks;


you''re not copying the content where toks points to into your array but you''re copying the pointer itself.
Meaning after this operation arr[i][j] points to the same memory as toks.

If you really want to copy the content where a char* points to you can use strcpy()[^]

Considering the explained, while doing this:

while(toks!=NULL)
{
     // after this the element arr[i][j] points to the same memory as toks
     arr[i][j]=toks;
     // you're writing the next token to the memory pointed to by toks (which is the same as arr[i][j] !)
     // In the next iteration toks still points to the same memory.
     // All of your elements in arr[i][j] as well as toks point to the same memory!
     toks = strtok(NULL,",");
     j++;
     col=j;
}



All your array elements point to the same memory which you''re overwriting with each call to strtok.

Another thing is. If you''re in an C++ environment why you''re not using the much safer std::string and std::vector?


Your code currently only copies pointers to tokens that you get through strtok. These tokens are stored in a static copy of your input string, which you stored in line. This means that each time you make a new call to strtok at the start of a new line, this static copy will be overwritten with the new line, and thus your previous pointers will all be invalidated: they still point into valid memory, but the contents therein are different!

Therefore, it may well be that your code worked as long as you only looked at a one-line input file. but the moment you start reading the second line and apply strtok to it, your program breaks.

As others have pointed out, it would be wiser to use std::string and std::vector like this:

std::vector<std::vector<std::string> > arr; // dynamic 2D array of dynamic strings

For just a quick fix, it would suffice to replace the char* from your declaration:

std::string arr[50][50]; // fixed size 2D array of dynamic strings

But note, that if you ever have more than 50 lines or more than 50 strings per line, your program will break!

Changing the type like this will result in changing the meaning of

arr[i][j] = toks;

from merely copying the pointer to actually copying the string that toks points to. This should be sufficient to fix your problem for now.


Basically you are handling badly the C-like strings, inserting temporary character pointers into your array.
Since you are using C++ why don''t you exploit its standard library?
Try, for instance:

#include<iostream>
#include <vector>
#include<fstream>
#include<string>
using namespace std;

int main()
{
  ifstream myfile("sample.csv");

  vector < vector< string > > ar;
  while (true)
  {
    string s;
    size_t lastpos, curpos;

    getline(myfile, s);
    if ( !myfile) break;
    ar.push_back( vector<string>());
    for (lastpos=curpos=0; curpos!=string::npos; lastpos=curpos+1)
    {
      curpos = s.find(',', lastpos);
      if (curpos != string::npos)
        ar.back().push_back(s.substr(lastpos, curpos-lastpos));
      else
        ar.back().push_back(s.substr(lastpos));
    }
  }

  myfile.close();

  for (size_t i=0; i < ar.size(); i++)
  {
    for (size_t j=0; j < ar[i].size(); j++)
    {
        cout << ar[i][j] << "\t";
    }
    cout << endl;
  }
  return 0;
}


这篇关于C ++中的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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