1. We can put a file into a string object, using the string class does not have to worry about memory allocation, but if we want to assign each row to a string, know how many instances of string are only known after reading.
2. If there is a "container" object that can accommodate all the different objects, the C + + standard container class, which is an important powerful tool for C + +.
3. People often confuse the "container" and "algorithm" in the standard library with what is known as the "STL" (Standard template class library). STL is a C + + library that is written by Alex's Hewlett-Packard employee, but it's good to compare systems and free promotion, and STL's code is broadly divided into three categories: algorithm (algorithm), container (container), and iterator (iterator), Almost all of the code takes the form of template classes and template functions, which provides a better opportunity for code reuse than traditional libraries that consist of functions and classes. In the C + + standard, STL is organized into the following 13 header files:<algorithm>, <deque>, <functional>, <iterator>, <vector>, <list>, <map>, <memory>, <numeric>, <queue>, <set>, <stack> and <utility >. STL is an extensive application of the concept of C + + "container" of the specific implementation.
4.vector the most basic standard container, the vector class is a template that can create a vector of shape, a vector of animal, a vector of string strings, and can create almost any class of things with a template.
Vector<string> a vector of string objects only
Getline () function prototype
istream& getline (IStream &is, String &str, char delim);istream& getline (istream&, string& is the input stream for the read-in Operation STR stores the read-in content Delim Terminator writes the characters read in the input stream is to Str until the Terminator Delim is encountered. For the first function Delim is a terminator that can be defined by the user itself, and for the second function Delim the default is ' \ n ' (newline character). The function ends when it encounters a file terminator (EOF) in the input stream is or encounters an error during the read-in character. After encountering Terminator Delim, Delim will be discarded and not stored in Str. The next read-in operation will begin reading in the next character of Delim.
//copy a file to a vector of string each row of a string#include <iostream>#include<fstream>#include<string>#include<vector>using namespacestd;intmain () {vector<string>V_str; Ifstream in ("Demo.cpp"); stringLine ; while(Getline (In,line))//getline () omit 3rd default argument "\ n" encounters a newline character to save a stringV_str.push_back (line);//Add the line to the end push_back () adds a new element to the vector//Add line Numbers for(intI=0; I<v_str.size (); i++) cout<<i<<":"<<v_str[i]<<endl;//with the operator "<<" overload, a simple and efficient subscript can be used for vector return 0;}/*Output:*/
A word is a string
#include <fstream>#include<iostream>#include<string>#include<vector>using namespacestd;intmain () {vector<string>words; Ifstream Filein ("GetWords.cpp"); stringWord; while(Filein>>word)//read in each string word takes a space to get the string objectWords.push_back (word);//add word to words//Output for(intI=0; I<words.size (); i++) cout<<words[i]<<Endl; return 0; }
Create a vector that holds integers
#include <vector>#include<iostream>using namespacestd;#defineN 20intmain () {vector<int>V_int; for(intI=0; i<n; i++)//There must be a destiny here .V_int.push_back (i);//i is int type for(intI=0; I<v_int.size (); i++) cout<<v_int[i]<<", "; cout<<Endl; for(intI=0; I<v_int.size (); i++) V_int[i]*=2; for(intI=0; I<v_int.size (); i++) cout<<v_int[i]<<", "; cout<<Endl; return 0;}
Standard container class container class in C + +