我的c++程序正在打印一个地址而不是一个值
My c++ program is printing out an address instead of a value
我有一个c++程序,可以从文件中读取矩阵。它将所有非零值和阈值以上的值存储在我创建的结构的2d向量中。该结构包含该值和该值的索引。当我打印出2d向量中的所有值时,它会打印出索引,但随后它会打印出来该值的地址。
这是代码本身:
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include <math.h>
struct NZ{
int index;
double val;
} nz;
using namespace std;
int main(int argc, const char* argv[]) {
string line;
int nonzero = 0;
int rowNum = 0;
double epsilon;
bool ep = false;
if (argc > 1 && string(argv[1]) == "-e")
{
epsilon = fabs(strtod(argv[2], 0));
ep = true;
}
vector< vector<NZ> > rows;
int linenum = -1;
while (getline(cin, line))
{
rowNum++;
rows.push_back( vector<NZ>() );
std::istringstream lstream(line) ;
double val;
linenum++;
int i = 1;
while (lstream>> val)
{
if(ep == true)
{
if (val != 0 && fabs(val) > epsilon){
nonzero++;
nz.val = val;
nz.index = i;
rows[linenum].push_back(nz);
}
}
else if (val != 0){
nonzero++;
nz.val = val;
nz.index = i;
rows[linenum].push_back(nz);
}
i++;
}
}
cout << rowNum << " " << nonzero;
for(int i=0; i<rows.size(); i++) {
cout << endl;
for(int j = 0;j<2;j++) {
cout<< rows[i][j].index << " " << cout<< rows[i][j].val;
}
}
}
这是输入文件:
0 -0.25 3 4
1 0.2 0.2 5
4 0 2 0.1
这就是它的输出:
3 6
3 0x60626834 0x6062684
1 0x60626814 0x6062685
1 0x60626843 0x6062682
这是它应该输出的:
3 6
3 3 4 4
1 1 4 5
1 4 3 2
第一行打印出文件中的行数和文件中非零数字的数目。其余的行打印出二维矢量。它首先打印数字的索引,然后打印数字本身。
尝试从此行删除cout
:
cout<< rows[i][j].index << " " << cout<< rows[i][j].val;
获取
cout<< rows[i][j].index << " " << rows[i][j].val;
相关文章:
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 当我们从/tp地址中添加/减去一个整数时会发生什么
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- ptrA = ptrB 是否等同于 ptrA = &*ptrB?,空值是否共享一个地址空间?
- 指向下一个地址的指针
- 若指针是一个地址,那个么什么是引用
- 表示一个地址需要多少内存
- 访问数组C++的最后一个地址的最安全方法
- 我的c++程序正在打印一个地址而不是一个值
- 是否有任何操作系统允许将内存从一个地址移动到另一个地址而不进行物理复制?
- 如果最后一个地址是0xFFFFFFFF,我如何获取数组结束后的地址
- 如何在c++中调用另一个地址空间中的函数
- 为什么"a->content"给我一个地址而不是一个值?
- 我如何发现什么写到一个地址使用c++
- 比起返回一个指针,返回一个地址能找到任何有用的应用吗?
- 访问存储在指针变量所指向的地址中的值(这是一个地址)返回垃圾值
- CreateFile()返回一个地址为FFFFFF的无效指针
- 当获取一个地址时,模板类型(类/函数)的实例化规则是什么?
- 获取从另一个地址指向的地址
- 成员函数指针-仅一个地址