我们是否可以将一个数组的内容复制到另一个数组中,并使用C#和C++;
基本上,我遇到了一个简单的问题 代码:我们是否可以将一个数组的内容复制到另一个数组中,并使用C#和C++;,c#,c++,arrays,dynamic,static,C#,C++,Arrays,Dynamic,Static,基本上,我遇到了一个简单的问题 代码: //的自我实现 C/C++中的向量类 #包括 使用名称空间std; 模板类向量类 { //arr是整数指针 //它存储向量的地址 T*arr; //容量是总存储量 //向量的容量 国际能力; //current是元素数 //目前存在于载体中 电流; 公众: //要初始化的默认构造函数 //初始容量为1个元件和 //使用动态分配分配存储 向量类() { arr=新的T[1]; 容量=1; 电流=0; } //函数在最后添加一个元素 无效推送(T数据) { /
//的自我实现
C/C++中的向量类
#包括
使用名称空间std;
模板类向量类
{
//arr是整数指针
//它存储向量的地址
T*arr;
//容量是总存储量
//向量的容量
国际能力;
//current是元素数
//目前存在于载体中
电流;
公众:
//要初始化的默认构造函数
//初始容量为1个元件和
//使用动态分配分配存储
向量类()
{
arr=新的T[1];
容量=1;
电流=0;
}
//函数在最后添加一个元素
无效推送(T数据)
{
//如果元素数等于
//容量,这意味着我们没有空间
//容纳更多的元素。我们需要将
//容量
如果(当前==容量){
T*温度=新的T[2*容量];
//将旧数组元素复制到新数组
对于(int i=0;i}
//函数获取向量的大小
int size(){返回当前;}
//函数获取向量的容量
int getcapacity(){返回容量;}
//用于打印数组元素的函数
作废打印()
{
对于(int i=0;i 在C++中,我们需要对动态内存进行管理,这与C<不同。
在上面的代码中,Push方法有一个语句arr=temp;该语句的意思是什么?它们是否试图将数组temp的内容复制到arr
这只是意味着将指针temp复制到arr,这里没有发生数组复制。这是一种所有权转移,temp处理的动态内存已转移到arr
如果arr是静态类型怎么办?像这样简单地将一个数组的内容复制到另一个数组会起作用吗?还是会与动态数组一起起作用
< C++ > C++中的所有变量都有静态类型,所有数组都需要用<代码> MycPy < /C> >或<>代码> STD::复制< <代码> >,也可以查看<代码>:ST::vector < /C> >,C++中,我们不经常使用动态数组,您的代码不是现代C++风格,我们更喜欢使用包含管理器动态阵列以外的其他ers(这使我们远离内存泄漏和某些类型的内存损坏)。我们可以使用=
复制向量,它更易于使用且更安全
在像C#这样的编程语言中,它也能工作吗
你可以引用这个伟大的例子。除了不需要管理内存之外,在某种程度上,它类似于C++ + < /P > 1。这是指针重新赋值。2。3。我对C语言不太了解,但是你不可能用那种语言写容器。“他们是不是试图把数组TEMP的内容复制到ARR?”--既然arr
不是一个数组,那么这些内容将流向何处?(不要考虑arr
是如何初始化的。孤立地考虑代码的这一部分。将某个内容分配给t*
变量,突然之间就必须有一个目标数组?)
// Self implementation of
// the Vector Class in C++
#include <bits/stdc++.h>
using namespace std;
template <typename T> class vectorClass
{
// arr is the integer pointer
// which stores the address of our vector
T* arr;
// capacity is the total storage
// capacity of the vector
int capacity;
// current is the number of elements
// currently present in the vector
int current;
public:
// Default constructor to initialise
// an initial capacity of 1 element and
// allocating storage using dynamic allocation
vectorClass()
{
arr = new T[1];
capacity = 1;
current = 0;
}
// Function to add an element at the last
void push(T data)
{
// if the number of elements is equal to the
// capacity, that means we don't have space to
// accommodate more elements. We need to double the
// capacity
if (current == capacity) {
T* temp = new T[2 * capacity];
// copying old array elements to new array
for (int i = 0; i < capacity; i++) {
temp[i] = arr[i];
}
// deleting previous array
delete[] arr;
capacity *= 2;
arr = temp;
}
// Inserting data
arr[current] = data;
current++;
}
// function to add element at any index
void push(int data, int index)
{
// if index is equal to capacity then this
// function is same as push defined above
if (index == capacity)
push(data);
else
arr[index] = data;
}
// function to extract element at any index
T get(int index)
{
// if index is within the range
if (index < current)
return arr[index];
}
// function to delete last element
void pop() { current--; }
// function to get size of the vector
int size() { return current; }
// function to get capacity of the vector
int getcapacity() { return capacity; }
// function to print array elements
void print()
{
for (int i = 0; i < current; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
};