Python的追加方法C++等价物是什么?

What is the C++ equivalent of Python's append method?

本文关键字:等价物 是什么 C++ 方法 追加 Python      更新时间:2023-10-16

考虑在python中使用以下代码

li = [1,2,3]
li.append(4)

如果现在调用 li,输出将为:

[1,2,3,4]

如果您使用 C++ 复制上面的代码,那会是什么?

我认为 C++11 等价物是这样的:

 #include <vector>
 int main(int argc, char ** argv)
 {
    std::vector<int> li = {1, 2, 3};
    li.push_back(4);
    return 0;
 }   
#include <iostream>
#include <string>
 
using namespace std;

int tl[3]={1,2,3};
int tl2[4]={};
class apnddd {        
  public:          
    int* ol;  
    int* nl;  
    int it;      
    apnddd(int* l1, int* l2, int it) { 
      ol = l1;
      int sz = sizeof(l1)/sizeof(l1[0]);
      for(int i=0;i<sz+1;i++) {
          l2[i]=l1[i];
          }
         l2[sz+1]=it;
      nl = l2;
      
      
    }
};
int main()
{
    
  apnddd d1(tl, tl2, 4);
 
  int* tl=d1.nl;
  std::cout <<  d1.nl[3]<<tl[3];
  
}