Arrays in Python & C++#

In Python, arrays are called lists. Lists are a very powerful data structure that can be used to store a wide variety of data. Lists can be used to store numbers, strings, other lists, and even functions. Lists are also very flexible, as they can be resized and their contents can be changed. Lists are also very easy to use, as they have a wide variety of built-in methods that can be used to manipulate them.

Creating a list#

To create a list, you simply need to put a comma-separated list of values in square brackets. For example:

>>> my_list = [1, 2, 3, 4, 5]
>>> my_list
[1, 2, 3, 4, 5]

C++ 例子#

 1#include <iostream> // header file for taking input and producing output
 2using namespace std;
 3
 4int main()
 5{
 6	int array[4] = {1, 3, 5, 7}; // initializing an array of size 4
 7	int *ptr;										 // declaring a pointer
 8
 9	cout << array[3] << "\n"; // gives the element at index 3
10	cout << array[2] << "\n"; // gives the element at index 2
11
12	cout << "==================================================\n";
13	cout << "Displaying the Address of The Pointer of an Array";
14	cout << "\n=================================================\n\n";
15
16	int i;
17	for (i = 0; i < 4; i++)
18	{
19		cout << "Address Of " << array[i] << " Using Array is ===> " << &array[i] << endl;
20	}
21
22	cout << "\n========================================\n";
23	return 0;
24}

运行

g++ array.cpp
./a.out