新手C++求教,for循环中初始化出有关问题
新手C++求教,for循环中初始化出问题
for循环中初始化出问题,代码如下:
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> a;
int temp;
while( cin >> temp ){
a.push_back( temp );
}
int *b = new int[ a.size() ];
int *p = b;
for ( vector<int>::const_iterator it = a.begin() /*int *p = b有这句就出错*/; it != a.end(); it++,p++ ){
*p = *it;
}
for( int *p1 = b; p1 != b + a.size(); p1++ ){
cout << *p1 << " ";
}
cout << endl;
delete [] b;
system( "pause" );
return 0;
}
------解决方案--------------------
只能写一个语句,所以只能声明一种类型。
------解决方案--------------------
for中第一个分号前的不同声明用逗号间隔...
------解决方案--------------------
for循环中初始化出问题,代码如下:
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> a;
int temp;
while( cin >> temp ){
a.push_back( temp );
}
int *b = new int[ a.size() ];
int *p = b;
for ( vector<int>::const_iterator it = a.begin() /*int *p = b有这句就出错*/; it != a.end(); it++,p++ ){
*p = *it;
}
for( int *p1 = b; p1 != b + a.size(); p1++ ){
cout << *p1 << " ";
}
cout << endl;
delete [] b;
system( "pause" );
return 0;
}
------解决方案--------------------
只能写一个语句,所以只能声明一种类型。
------解决方案--------------------
for中第一个分号前的不同声明用逗号间隔...
------解决方案--------------------
- C/C++ code
vector<int>::const_iterator it = a.begin(); for ( int *p = b; it != a.end(); it++,p++ ) { *p = *it; } //for初始化,类型不一样。