C++ study notes (1)-the use of vector

vector container

A vector is a container similar to an array, which can hold many types of data, such as several integers, with more comprehensive content than an array. When using it, you need to include the header file: #include; many operations have their own functions that can be used directly. The main functions include:

v.push_back(k);//尾插元素
v.insert(it,k);//在任意位置插入元素
v.eraser(it,it+k);//删除任意元素
v.size();//容量
v.clear();//清空容器
v.empty();//判断容器是否为空
reverse(v.begin(),v.end());//反转任意段元素
sort(v,begin(),v.end(),cmp);//sort排序默认由小到大,cmp可以自由规定排序规则。

1. Initialization of vector

(1)vector<int> a(10);
//定义了10个整型元素的向量(尖括号中元素类姓名,它可以是任何合法的数据类型),但没有给出初值,其值不确定
(2)vector<int> a(10,1);
//定义了10个整型元素的向量,且每个元素的初值为1
(3)vector<int> a(b)
//用b向量来创建a向量整体复制性赋值
(4)vector<int> a(b.begin(),b.begin+3);
//定义了a值为b中第0个到第2个(共3个)元素
(5)int b[7]={1,2,3,4,5,6,7};
     vector<int> a(b,b+7);
//从数组中获得初值

Two, several important operations of vector objects 

(1)a.assign(b.begin(),b.begin()+3);//b为向量,将b的0~2个元素构成的向量赋给a
(2)a.assign(4,2);//a只含4个元素,且每个元素为2
(3)a.back();//返回a的最后一个元素
(4)a.front();//返回a的第一个元素
(5)a[i];//返回a的第i个元素,当且仅当a[i]存在
(6)a.clear();//清空a中的元素
(7)a.empty();//判断a是否为空,空则返回true,不空则返回false
(8)a.pop_back();//删除a向量的最后一个元素
(9)a.erase(a.begin()+1,a.begin()+3);//删除a中第1个(从第0个算起)到第2个元素,也就是说删除的元素从a.begin()+1算起(包括它)一直到a.begin()+3(不包括它)
(10)a.push_back(5);//在a的最后一个向量后插入一个元素,其值为5
(11)a.insert(a.begin()+1,5);//在a的第1个元素(从第0个算起)的位置插入数值5,如a为1,2,3,4,插入元素后为1,5,2,3,4
(12)a.insert(a.begin()+1,3,5);//在a的第1个元素(从第0个算起)的位置插入3个数,其值都为5
(13)a.insert(a.begin()+1,b+3,b+6);//b为数组,在a的第1个元素(从第0个算起)的位置插入b的第3个元素到第5个元素(不包括b+6),如b为1,2,3,4,5,9,8,插入元素后为1,4,5,9,2,3,4,5,9,8
(14)a.size();//返回a中元素的个数,
(15)a.capacity();//返回a在内存中共可以容纳的元素个数
(16)a.resize(10);//将a的现有元素个数调至10个,多则删,少则补,其值随机
(17)a.resize(10,2);//将a的现有元素个数调至10个,多则删,少则补,其值为2
(18)a.reserve(100);//将a的容量(capacity)扩充至100,也就是说现在测试a.capacity();
(19)a.swap(b);//b为向量,将a中的元素和b中的元素进行整体性交换
(20)a==b;//b为向量,向量的比较操作还有!=,>=,<=,>,<

Three, several important algorithms, need to include the header file when using

#include<algorithm>
(1)sort(a.begin(),a.end());
//对a中的从a.begin()(包括它)到a.end(不包括它)的元素进行从小到大排列
(2)reverse(a.begin(),a.end());
//对a中的从a.begin()(包括它)到a.end(不包括它)的元素倒置,但不排列,如a中的元素为1,3,2,4,倒置后为4,2,3,1
(3)copy(a.begin(),a.end(),b.begin()+1);
//对a中的从a.begin()(包括它))到a.end(不包括它)的元素复制到b中,从b.begin()+1的位置(包括它)开始复制,覆盖掉原有元素
4)find(a.begin(),a.end(),10);
//在a中的从a.begin()(包括它)到a.end()(不包括它)的元素中查找10,若存在返回其在向量中的位置

Four, several ways to access vector sequentially

(1) Add elements to vector a

1. Simple

vector<int> a;
for(int i=0;i<10;i++)
a.push_back(i);

2. Select elements from the array to add to the vector

int a[6] = {1,2,3,4,5,6};
vector<int> b;
for(int i=1; i<=4; i++)
b.push_back(a[i]);

3. Select elements from the existing vector to add to the vector

int a[6] = {1,2,3,4,5,6};
vector<int> b;
vector<int> c(a,a+4);
for(vector<int>::iterator it=c.begin();it<c.end();it++)
b.push_back(*it);

4. Read elements from the file and add them to the vector

ifstream in("data.txt");
vector<int> a;
for(int i; in>>i)
    a.push_back(i);

5. [Misunderstanding]

vector<int> a;
for(int i=0;i<10;i++)
    a[i] = i;
//这种做法是错误的,下标只能用于获取已存在的元素,而现在的a[i]还是空的对象。

(2) Read elements from the vector

1. Read by subscript

int a[6]={1,2,3,4,5,6};
vector<int> b(a,a+4);
for(int i=0;i<=b.size()-1;i++)
    cout<<b[i]<<" ";

2. Read by way of traverser

int a[6]={1,2,3,4,5,6};
vector<int> b(a,a+4);
for(vector<int>::iterator it=b.begin();it!=b.end();it++)
    cout<<*it<<" ";

Five, the use of fill() in C++

When we want to fill the value of a container, we can use the fill() function.

Use fill() function to fill ordinary one-dimensional array

  • code show as below
#include <iostream>     //std::cout
#include <algorithm>    //std::fill

using namespace std;

int main(){
   int array[8];                     //myvector:0 0 0 0 0 0 0 0 
   

   cout<<"========begin========"<<"\n";
   for(int i = 0 ; i < 8 ; i++)
   cout << ' ' << array[i];
   cout << '\n'<<'\n';

   fill (array,array+4,5);         //myvector: 5 5 5 5 0 0 0 0
   fill (array+3,array+6,8);       //myvector: 5 5 5 8 8 8 0 0

   cout<<"========after fill========"<<"\n";
   for(int i = 0 ; i < 8 ; i++)
   cout << ' ' << array[i];
   cout << '\n'<<'\n';

   return 0;
} 
  • Results of the:
=======begin=======
 -1 -1 4253733 0 1 0 4254665 0

=======after fill=======
 5 5 5 8 8 8 4254665 0

Need to pay attention to

  • Because it is vectorno longer an ordinary array (even if it can be regarded as an array), we don't need to use the first address of the array, because the vector has already encapsulated the method for us, its initial address is vector.begin(), and the last address is vector.end(). The rest is the same array.

[One question per day]

Find common characters

Given a string array A consisting of only lowercase letters, return a list of all characters ( including repeated characters ) displayed in each string in the list. For example, if a character appears 3 times in each string, but not 4 times, you need to include the character 3 times in the final answer.

You can return the answers in any order.

Example 1:

输入:["bella","label","roller"]
输出:["e","l","l"]

Example 2:

输入:["cool","lock","cook"]
输出:["c","o"]

【answer】

Ideas and methods:
According to the requirements of the question, if the character c appears k times or more in all strings, then the final answer needs to include k cs. Therefore, we can use minfreq[c] to store the minimum number of occurrences of the character c in all strings.

We can iterate through each string in turn. When we traverse the string s, we use freq[c] to count the number of occurrences of each character s in s. After the statistics are completed, we then update each minfreq[c] to the smaller value of itself and freq[c]. In this way, when we traverse all strings, minfreq[c] stores the minimum number of occurrences of character c in all strings.

Since the title guarantees that all characters are lowercase letters, we can use an array of length 26 to represent minfreq and freq respectively.

When constructing the final answer, we traverse all the lowercase letters c and add minfreq[c] cs to the answer array.

Code:

class Solution {
public:
    vector<string> commonChars(vector<string>& A) {
        vector<int> minfreq(26, INT_MAX);
        vector<int> freq(26);
        for (const string& word: A) {
            fill(freq.begin(), freq.end(), 0);
            for (char ch: word) {
                ++freq[ch - 'a'];
            }
            for (int i = 0; i < 26; ++i) {
                minfreq[i] = min(minfreq[i], freq[i]);
            }
        }

        vector<string> ans;
        for (int i = 0; i < 26; ++i) {
            for (int j = 0; j < minfreq[i]; ++j) {
                ans.emplace_back(1, i + 'a');
            }
        }
        return ans;
    }
};

 

Guess you like

Origin blog.csdn.net/weixin_38452841/article/details/109068324