0


隣接ノードのリストからグラフの隣接行列を作成し、ファイル(重みは不要)にC++のビットセット配列に格納 するタスクがあります。ファイルから隣接ノードを正常に読み取りましたが、ビットセット配列に格納しようとすると、結果が正しくありません。私の機能は次のとおりです。

bitset<N>* read_graph(string filepath)
{
    FILE *fp;
    char line[100];
    bitset<N> bs[N];

    fp = fopen(filepath.c_str(), "r");

    if(fp != NULL)
    {
       while(!feof(fp))
       {   
          fgets(line, 100, fp);
          //cout << line << endl;
          if(line[0] == 'a')
          {
               string str = ""; 
               int i(0);    
               for(i = 2; line[i] != ' '; i++)
               {
                  if(line[i] != ' ')
                  {
                    str += line[i];
                  }     
               }
               int fi = atoi(str.c_str());
               i++;
               str = "";
               for(int j = i; line[j] != ' '; j++)
               {
                  if(line[j] != ' ')
                  {
                    str += line[j];
                  }     
               }    
               int si = atoi(str.c_str());
               si--;
               fi--;
               //cout << "string: " << str << endl;
               cout << "fi: " << fi;
               //cout << "string: " << str << endl;
               cout << "   si: " << si << endl;
               bs[fi][si]= 1;
          }
       }          
    }
    fclose(fp);

    return bs;
}

結果は次のようになります(fiは最初のインデックスを表し、siは2番目のインデックスを表します)。

sample.gr
fi:0 si:1
fi:0 si:2
fi:1 si:3
fi:2 si:4
fi:3 si:2
fi:3 si:5
fi:4 si:1
fi:4 si: 5
fi:4 si:5

000000
000001
011000
001000
000000
000000

The indexes are right, I've checked them, but the matrix should be the following (it is mirrored because of the bitset's right side positioning):

000000
010001
001001
000010
000100
011000

I guess the error is somewhere around the bitset element accessing but I cannot find out what exactly is wrong.

I appreciate any help. Thanks.

4

2 に答える 2

2

ローカル配列へのポインタを返しています。未定義動作。

vector<bitset<N> >代わりにまたは同様のものを使用してください。

于 2011-03-31T19:40:12.423 に答える
2

With the pointer to a local array problem fixed, your code runs for me and prints what's expected (except mirrored).

But wouldn't it be easier to use C++ I/O in this case?

#include <vector>
#include <bitset>
#include <iterator>
#include <fstream>
#include <sstream>
#include <iostream>
const int N=6;
std::vector<std::bitset<N> > read_graph(const std::string& filepath)
{
    std::vector<std::bitset<N> > bs(N);
    std::ifstream fp(filepath.c_str());
    for(std::string line; getline(fp, line); )
    {
        if(line.size() > 1 && line[0] == 'a')
        {
            std::istringstream str(line.substr(1));
            int fi, si;
            str >> fi >> si;
            std::cout << "fi: " << fi << "   si: " << si << '\n';
            bs[--fi][--si]= 1;
        }
    }
    return bs;
}
int main()
{
    std::vector<std::bitset<N> > v = read_graph("sample.gr");
    copy(v.rbegin(), v.rend(),
         std::ostream_iterator<std::bitset<N> >(std::cout, "\n"));
}

test run: https://ideone.com/z7Had

于 2011-03-31T19:56:06.157 に答える