本文地址:http://www.cnblogs.com/archimedes/p/cpp-primer-chapter4-ans.html,轉載請注明源地址。
【習題 4.7】
編寫必要的代碼將一個數組賦給另一個數組,然後把這段代碼改用 vector 實現。 考慮如何將一個 vector 賦給另一個 vector。
用數組實現:
#include <iostream>
using namespace std;
int main( )
{
const size_t size=5;
int a1[size]={0,1,2,3,4};
int a2[size];
for(size_t i=0; i<size; ++i)
a2[i]=a1[i];
system("PAUSE");
return 0;
}
用vector實現:
#include <iostream>
#include <vector>
using namespace std;
int main( )
{
int a[5]={1,2,3,4,5};
vector<int> vec1(a,a+5);
vector<int> vec2;
for(vector<int>::iterator it=vec1.begin(); it!=vec1.end(); ++it)
vec2.push_back(*it);
system("PAUSE");
return 0;
}
【習題 4.8】
編寫程序判斷兩個數組是否相等,然後編寫一段類似的程序比較兩個 vector。
bool judge1(int *a, int *b, int n)
{
for(size_t i=0; i<n; i++) {
if(a[i]!=b[i])
return false;
}
return true;
}
比較vector:
bool judge2(vector<int> a, vector<int> b)
{
for(vector<int>::size_type it=0; it<a.size(); it++) {
if(a[it]!=b[it])
return false;
}
return true;
}
【習題 4.9】
編寫程序定義一個有 10 個 int 型元素的數組,並以其在數組中的位置作為各元素的初值。
#include <iostream>
using namespace std;
int main( )
{
int a[10];
for(size_t i=0; i<10; i++) {
a[i]=i;
cout<<a[i]<<" ";
}
cout<<endl;
system("PAUSE");
return 0;
}
【習題 4.14】
編寫代碼修改指針的值;然後再編寫代碼修改指針所指對象的值。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main( )
{
int *p;
int a=1;
int b=2;
p=&a;
cout<<p<<endl;
p=&b;
cout<<p<<endl;
*p=12;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
system("PAUSE");
return 0;
}
【習題 4.18】
編寫程序,使用指針把一個 int 型數組的所有元素設置為 0。
#include <iostream>
using namespace std;
int main( )
{
int a[5]={1,2,3,4,5};
for(int*p=a; p<a+5; p++)
*p=0;
for(int *p=a; p<a+5; p++)
cout<<*p<<endl;
system("PAUSE");
return 0;
}
【習題 4.25】
編寫程序比較兩個 string 類型的字符串,然後編寫另一個程序比較兩個 C 風格字符串的值。
#include <iostream> #include <string>
using namespace std; int main( ) { string s1,s2; cout<<"輸入兩個字符串:\n"; cin>>s1>>s2; if(s1>s2) cout<<"\""<<s1<<"\""<<"is bigger than"<<"\""<<s2<<"\""<<endl; else if(s1>s2) cout<<"\""<<s2<<"\""<<"is bigger than"<<"\""<<s1<<"\""<<endl; else cout<<"thay are equal"<<endl; system("PAUSE"); return 0; }
比較兩個 C 風格字符串:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main( )
{
const int size=80;
char *s1,*s2;
s1=new char[size];
s2=new char[size];
if(s1==NULL || s2==NULL) {
cout<<"No enough memory!"<<endl;
return -1;
}
cout<<"Enter two strings:"<<endl;
cin>>s1>>s2;
int result;
result=strcmp(s1,s2);
if(result>0)
cout<<"\""<<s1<<"\""<<"is bigger than"<<"\""<<s2<<"\""<<endl;
else if(result<0)
cout<<"\""<<s2<<"\""<<"is bigger than"<<"\""<<s1<<"\""<<endl;
else
cout<<"thay are equal"<<endl;
delete []s1;
delete []s2;
system("PAUSE");
return 0;
}
【習題 4.28】
編寫程序由從標准輸入設備讀入的元素數據建立一個 int 型 vector 對象,然後動態創建一個與該 vector 對象大小一致的數組,把 vector 對象的所有元素復制給新數組。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main( )
{
vector<int> vec;
int n;
cout<<"請輸入數字:"<<endl;
while(cin>>n)
vec.push_back(n);
int size=vec.size();
int *a=new int[size];
for(vector<int>::size_type i=0; i<size; i++) {
a[i]=vec[i];
}
cout<<"符合要求的數組為:";
for(int i=0; i<size; i++)
cout<<a[i]<<endl;
delete []a;
system("PAUSE");
return 0;
}
【習題 4.30】
編寫程序連接兩個 C 風格字符串字面值,把結果存儲在一個 C 風格字符串中。然後再編寫程序連接兩個 string 類型字符串,這兩個 string 類型字符串與前面 的 C 風格字符串字面值具有相同的內容。
#include <iostream>
#include <cstring>
using namespace std;
int main( )
{
const char *s1="hello ";
const char *s2="world.";
size_t len=strlen(s1)+strlen(s2);
char *res=new char[len+1];
strcpy(res,s1);
strcat(res,s2);
cout<<res<<endl;
delete []res;
system("PAUSE");
return 0;
}
改進後的代碼:
#include <iostream>
#include <string>
using namespace std;
int main( )
{
const string s1("hello ");
const string s2("world.");
string res;
res=s1;
res+=s2;
cout<<res<<endl;
system("PAUSE");
return 0;
}
【習題 4.31】
編寫程序從標准輸入設備讀入字符串,並把該串存放在字符數組中。描述你的程序如何處理可變長的輸入。提供比你分配的數組長度長的字符串數據測試你的程序。
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main( )
{
string instr;
const size_t size=10;
char restr[size];
cout<<"請輸入字符串(<="<<size<<"個字符):"<<endl;
cin>>instr;
size_t len=strlen(instr.c_str());
if(len>size) len=size;
strncpy(restr,instr.c_str(),len);
restr[len+1]='\0';
return 0;
}
【習題 4.32】
編寫程序用 int 型數組初始化 vector 對象。
#include<iostream>
#include<vector>
using namespace std;
int main( )
{
const size_t arr_size=9;
int int_arr[arr_size];
cout<<"請輸入:"<<arr_size<<"個元素:"<<endl;
for(size_t i=0; i!=arr_size; i++)
cin>>int_arr[i];
vector<int> ivec(int_arr, int_arr+arr_size);
system("PAUSE");
return 0;
}
【習題 4.33】
編寫程序把 int 型 vector 復制給 int 型數組。
#include<iostream>
#include<vector>
using namespace std;
int main( )
{
vector<int> ivec;
int n;
cout<<"請輸入數字:"<<endl;
while(cin>>n)
ivec.push_back(n);
int *a=new int[ivec.size()];
size_t i=0;
for(vector<int>::iterator it=ivec.begin(); it!=ivec.end(); ++it,++i)
a[i]=*it;
delete []a;
system("PAUSE");
return 0;
}
【習題 4.34】
編寫程序讀入一組 string 類型的數據,並將它們存儲在 vector 中。接著,把該 vector 對象復制給一個字符指針數組。為 vector 中的每個元素創建一個新的字符數組,並把該 vector 元素的數據復制到相應的字符數組中,最後把指向 該數組的指針插入字符指針數組。
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
int main( )
{
vector<string> svec;
string str;
cout<<"請輸入字符串:"<<endl;
while(cin>>str)
svec.push_back(str);
char **arr = new char*[svec.size()];
size_t i=0;
for(vector<string>::iterator it=svec.begin(); it!=svec.end(); it++) {
char *p=new char[(*it).size()+1];
strcpy(p, (*it).c_str());
arr[i]=p;
}
for(i=0; i!=svec.size(); i++)
delete []arr;
system("PAUSE");
return 0;
}