zoukankan      html  css  js  c++  java
  • C++ vector 容器

     1 //vector类  resemble array  自动扩容...  暂存于内存中
     2 //格式 vector<类(型)名> 对象名  example: vector<string> v; vector<int> i;
     3 //copy a entire file into a vector of string
     4 #include<fstream>
     5 #include<string>
     6 #include<iostream>
     7 #include<vector>
     8 using namespace std;
     9 int main()
    10 {
    11  vector<string> v;
    12  string  s;
    13  ofstream out("file.txt");
    14  for (int i = 0; i < 3; i++)
    15  {
    16   cout << "Please enter:" << endl;
    17   getline(cin, s);
    18   out << s << '
    ';
    19  }
    20  out.close();
    21  ifstream in("file.txt");
    22  while (getline(in, s))//按行读取
    23   v.push_back(s);  //将string s压入vector v的尾部
    24  for (int i = 0; i < v.size(); i++)
    25   cout <<i<<":"<< v[i] << endl;
    26  for (int i = 0; i < v.size(); i++)
    27   v[i] += "!";//可以根据下标改变单元的值
    28  for (int i = 0; i < v.size(); i++)
    29   cout << i << ":" << v[i] << endl;
    30  system("pause");
    31  return 0;
    32 }
  • 相关阅读:
    Largest Rectangle in Histogram
    Valid Sudoku
    Set Matrix Zeroes
    Unique Paths
    Binary Tree Level Order Traversal II
    Binary Tree Level Order Traversal
    Path Sum II
    Path Sum
    Validate Binary Search Tree
    新手程序员 e
  • 原文地址:https://www.cnblogs.com/hzhqiang/p/9607423.html
Copyright © 2011-2022 走看看