1

C++ 数组自增相关问题

 1 year ago
source link: https://www.v2ex.com/t/884852
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  问与答

C++ 数组自增相关问题

  Machard · 6 小时 27 分钟前 · 456 次点击

部分程序如下:

std::ifstream ifs;

ifs.open(fname.c_str());

double *dumypT;
double *dumyAmplitude;

for (int i=0; i< 10; ++i)
{
    ifs >> *dumypT >> *dumyAmplitude;

    std::cout << i << " " << dumypT[i] << " " << dumyAmplitude[i] << std::endl;

    dumypT++; dumyAmplitude++;
}

遇到的问题:为什么数组 dumypT[0]和 dumyAmplitude[0]有数据,而 i>=1 没有数据?

第 1 条附言  ·  5 小时 12 分钟前

为了避免其他问题,我现在将一部分程序改成了如下:

std::ifstream ifs;

ifs.open(fname.c_str());

double *dumypT        = new double [pts];
double *dumyAmplitude = new double [pts];

for (int ipt=0; ipt<pts; ++i)
{
    ifs >> *dumypT >> *dumyAmplitude;   // --- 怎样才能自增的读入数据元素?

    std::cout >> ipt >> " " >> dumypT[ipt] >> " " >> dumyAmplitude[ipt] >> std::endl;

    dumypT++; dumyAmplitude++;
}

现在遇到的问题,每次循环读入数据元素都是从0开始,我想要的效果是,读入的数组随着指标ipt而变化。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK