vector string及数组使用

使用vector输入字符串并输出字符串:
#include
#include
#include
using namespace std;
int main()
{
 vector ivec;
 ivec.push_back("wo");
 ivec.push_back("shi");
 for (vector::iterator ix =ivec.begin(); ix != ivec.end(); ix++)
 {
  cout << *ix;
 }
 cout << endl;
 for (vector::size_type ix = 0; ix != ivec.size(); ix++)
 {
  cout << ivec[ix];
 }
 int ival;
 while (cin >> ival&&ival != 1)
 {
  cout << "haha "<< endl;
 }
}




  将一个数组赋值给另一个数组:
#include
#include
using namespace std;
int main()
{
 int a[5] = { 1, 2, 4, 5, 6 }, b[5];
 for (int i = 0; i <= 4; i++)
 {
  b[i] = a[i];
 }
 for (int i = 0; i <= 4; i++)
 {
  cout << b[i] << " ";
 }
 cout << endl;
 vector  ivec1(10, 2);
 vector ivec2;
 /*for (vector::iterator ix = ivec1.begin(); ix != ivec1.end(); ix++)
 {
  ivec2.push_back(*ix);
 }*/
 for (vector::size_type ix=0; ix != ivec1.size(); ix++)
 {
 ivec2.push_back(ivec1[ix]);
 }
 for (vector::iterator ix = ivec2.begin(); ix != ivec2.end(); ix++)
 {
  cout << ivec2[*ix]<<" ";
 }
 cout << endl;
}


 
指针的使用:
#include
using namespace std;
#include
int main()
{
 string s = ("i love you");
 string *p = &s;
 cout << s << endl << *p << endl;
 int ival = 1024, ival2 = 2048;
 int *p1=&ival, *p2=&ival2;
 *p1 = *p2;
 cout << *p1 << endl << *p2 << endl;
 int &ri = ival, &ri2 = ival2;
 ri = ri2;
 cout << ri << endl << ri2 << endl<


输入数字并输出:
#include
#include
#include
using namespace std;
 
int main()
{
 vector ivec;
 int k;
 while (cin >> k)
 {
  ivec.push_back(k);
 }
 int *p = new int[ivec.size()];
 int *p1 = p;
 for (vector::iterator iter = ivec.begin(); iter != ivec.end(); iter++, p1++)
 {
  *p1 = *iter;
 }
 for (vector::iterator iter = ivec.begin(); iter != ivec.end(); iter++)
 {
  cout << *iter << " ";
 }
 cout << endl;
 for (int i = 0; i <= 10; i++)
 {
  cout << p[i]<<" ";
 }
}
 
#include
#include
#include
using namespace std;
 
int main()
{
 const int k = 2;
 int a[k];
 for (size_t i = 0; i > a[i];
 
 }
 vector ivec;
 for (vector::size_type ix = 0; ix != k; ix++)
 {
  ivec.push_back(a[ix]);
 }
 for (vector::size_type ix = 0; ix != k; ix++)
 {
  cout << ivec[ix] << " ";
 }
 cout << endl;
}


你可能感兴趣的:(c++学习)