// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
//C++ primer assignment 4.33
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> ivec;
int ival;
// Input vector element
cout<<"Enter numbers: (Ctrl+z to end)"<<endl;
while (cin>>ival)
{
ivec.push_back(ival);
}
//create arrays
int *parr=new int(ivec.size());
//copy elements
size_t ix=0;
for (vector<int>::iterator iter=ivec.begin();ivec!=ivec.end();++iter,++ix)
{
parr[ix]=*iter;
}
delete []parr;
return 0;
}
// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
//C++ primer assignment 4.34
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> svec;
string str;
while (cin>>str)
{
svec.push_back(str);
}
//create char** array
char **parr=new char *[svec.size()];
//process vector element
size_t ix=0;
for (vector<string>::iterator iter=svec.begin();iter!=svec.end();++iter,++ix)
{
// create char array
char *p=new char[(*iter).size()+1];
// copy vector element to char array
strcpy(p,(*iter).c_str());
// parr[]
parr[ix]=p;
}
//release arrays
for (ix=0;ix!=svec.size();++ix)
{
delete[] parr[ix];
}
delete []parr;
return 0;
}
// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
//C++ primer assignment 4.35
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> svec;
string str:
cout<<"Enter the string"<<endl;
while (cin>>str)
{
svec.push_back(str);
}
//create char** array
char **parr=new char *[svec.size()];
// process vector
size_t ix=0;
for (vector<string>::iterator iter=svec.begin();iter!=svec.end();++iter,++ix)
{
char *p=new char[(*iter).size()+1];
// copy vector to arr
strcpy(p,(*iter).c_str());
parr[ix]=p;
}
//output the vector element
cout<<"Content of vector:"<<endl;
for (vector<string>::iterator iter2=svec.begin();iter2!=svec.end();++iter2)
{
cout<<"Out put the iter content"<<*iter2<<endl;
}
cout<<"Content of array"<<endl;
for (ix=0;ix!=svec.size();++ix)
{
cout<<parr[ix]<<endl;
}
//release the array
for (ix=0;ix!=svec.size();++ix)
{
delete []parr[ix];
}
delete []parr;
return 0;
}
// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
//C++ primer assignment 4.35
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int ia[3][4]={
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
int (*p)[4];
for (p=ia;p!=ia+3;++p)
{
for (int *q=*p;q!=*p+4;++q)
{
cout<<*q<<endl;
}
}
return 0;
}