std::istream& operator >> 和自己的向量 class
std::istream& operator >> and own vector class
我正在尝试使用一种自动创建没有给定大小的向量的方法编写向量 class:
std::istream& operator >> ( std::istream& is, Vector& v )
{
/* working (but not mine) version i am basing on
int size;
is >> size;
v.create( size );
for( int i=0; i<v.size(); i++ ) is >> v.m_pBuf[i];
return is;
*/
int size;
std::string strBuf;
//std::istream& tmp = is; //copy doesn't exist!
//is.ignore(0,'\n');
getline(is, strBuf,'\n');
size = (strBuf.length()+1)/2;
std::cout << "Size of buffer = " <<size<< std::endl;
std::cout << "bufrer = " <<strBuf<< std::endl;
v.create( size );
for( int i=0; i<v.size()*2; i++ ) {
if (!(i%2)){ // to avoid spaces
double vec = (double)strBuf[i]-48;
//std::cout << "vec = " <<vec<< std::endl;
v.m_pBuf[i]= vec;
}
}
return is;
}
但它因错误而崩溃:
/*
input:
Vector x(3);
x[0] = 1;
x[1] = 2;
x[2] = 3;
Vector y(x);
Vector z;
std::cout << "x=" << x << "y=" << y << "z=" << z;
std::cout << "Podaj wektor z: ";
std::cin >> z;
std::cout << "z=" << z;
output:
x=[ 1 2 3 ]
y=[ 1 2 3 ]
z=[ ]
Insert a vector z: 2 3 4 5 6 7
Size of buffer = 6
buffer = 2 3 4 5 6 7
z=[ 2 0 3 0 4 0 ]
Process returned -1073741819 (0xC0000005) execution time : 44.491 s
Press any key to continue.
*/
有什么技巧可以冻结我的 'is' 变量或重写输入流吗?到底哪里出了问题?
在你的 for 循环中,你正在根据 i
索引 v.m_pBuf
,你跳过了所有奇数 i
。因此,您正在尝试访问位置 0, 2, 4, ...
,这意味着您将超过分配的 space。尝试使用不同的索引变量并在 if
条件内递增它。
您的代码将如下所示:
for( int i=0, j=0; i<v.size()*2; i++ ) {
if (!(i%2)){ // to avoid spaces
double vec = (double)strBuf[i]-48;
//std::cout << "vec = " <<vec<< std::endl;
v.m_pBuf[j]= vec;
j++; // notice j increments only for even positions.
}
}
我正在尝试使用一种自动创建没有给定大小的向量的方法编写向量 class:
std::istream& operator >> ( std::istream& is, Vector& v )
{
/* working (but not mine) version i am basing on
int size;
is >> size;
v.create( size );
for( int i=0; i<v.size(); i++ ) is >> v.m_pBuf[i];
return is;
*/
int size;
std::string strBuf;
//std::istream& tmp = is; //copy doesn't exist!
//is.ignore(0,'\n');
getline(is, strBuf,'\n');
size = (strBuf.length()+1)/2;
std::cout << "Size of buffer = " <<size<< std::endl;
std::cout << "bufrer = " <<strBuf<< std::endl;
v.create( size );
for( int i=0; i<v.size()*2; i++ ) {
if (!(i%2)){ // to avoid spaces
double vec = (double)strBuf[i]-48;
//std::cout << "vec = " <<vec<< std::endl;
v.m_pBuf[i]= vec;
}
}
return is;
}
但它因错误而崩溃:
/*
input:
Vector x(3);
x[0] = 1;
x[1] = 2;
x[2] = 3;
Vector y(x);
Vector z;
std::cout << "x=" << x << "y=" << y << "z=" << z;
std::cout << "Podaj wektor z: ";
std::cin >> z;
std::cout << "z=" << z;
output:
x=[ 1 2 3 ]
y=[ 1 2 3 ]
z=[ ]
Insert a vector z: 2 3 4 5 6 7
Size of buffer = 6
buffer = 2 3 4 5 6 7
z=[ 2 0 3 0 4 0 ]
Process returned -1073741819 (0xC0000005) execution time : 44.491 s
Press any key to continue.
*/
有什么技巧可以冻结我的 'is' 变量或重写输入流吗?到底哪里出了问题?
在你的 for 循环中,你正在根据 i
索引 v.m_pBuf
,你跳过了所有奇数 i
。因此,您正在尝试访问位置 0, 2, 4, ...
,这意味着您将超过分配的 space。尝试使用不同的索引变量并在 if
条件内递增它。
您的代码将如下所示:
for( int i=0, j=0; i<v.size()*2; i++ ) {
if (!(i%2)){ // to avoid spaces
double vec = (double)strBuf[i]-48;
//std::cout << "vec = " <<vec<< std::endl;
v.m_pBuf[j]= vec;
j++; // notice j increments only for even positions.
}
}