I created a list. The point is this: I want each value I enter to be assigned each time a new variable. Approximately like this: x1, x2, ... xn. But then I could use them, for example, x5 + x55, etc. How to number so "x"? But that he automatically appropriated indexes. Hope I could get my message across to you. If not, please check with me. He tried, but nothing came to mind. Without using an array.

  • @ navi1893, if you understand correctly, then you are unable to assign the consecutive numbers in the list to the elements of the list that you are building. To do this, in the structure that describes the entire list, create a field, say, int maxnum and zero it when creating the list. In the structure of the list element, respectively. field int num. When you add a new item to the list, assign num = maxnum ++. Something like that. Then by these numbers you can search in the list (but only sequentially). - avp
  • and if I do, num = max ++, then I can't arbitrarily take the fifth element and the 100th for example. Not? The problem itself is as follows: I have a list, of type string. And I have only one variable X. Every time I enter the value of X and it is added to the list and displayed. But right now I need to take some element, for example the fifth one and assign it to the 20th. But I do not know how I can choose the very fifth and 20th elements to work with them. That's what my problem is all about. What advise me? - navi1893
  • If you need to access data by index, then the list is no good, you must use an array (vector). But, quickly choose - slowly remove (include at the beginning or middle), etc. In general, the choice of a data structure (architecture) depends on the frequency of access operations of one type or another to data elements in a specific task. Describe the task as a whole . You ask questions for some special cases. As a result, you get the correct, but unconsistent answers. - avp
  • Here is the task itself: It is required to implement the ability to work with strings of arbitrary length. The string is stored in separate pieces of fixed length, which are linked to the list and added or removed as needed. The class implementation must support the following. Features: 1) Create an empty buffer 2) Add the specified string to the end of the buffer 3) Insert the specified string from the specified buffer position 4) Get the length of the string accumulated in the buffer 5) Copy the string to the specified character array 6) Get / change character at the specified position 7) Replace one substring with another in the buffer - navi1893
  • Make a wrapper (your class) over the vector - avp

2 answers 2

You almost correctly described. A bit incorrect. In the array, the data is arranged by indices and have a specific memory cell in which the value is stored. You can access them by index (eg A[индекс] ), as well as perform actions ( A[индекс1]+A[индекс2] ). Here is a simple example of reading values ​​from a file into an array.

 int sizeA; //Размеры вектора ifstream in("in.txt"); //Инициализируем чтение файла in.txt if (!in) { //Если есть ошибка чтения cout << "Error reading file data" << endl; //Уведомим return 1; } in >> sizeA; //Считываем размер вектора А int A[sizeA]; //Инициализируем вектор заданного размера for (int i = 0; i <= sizeA - 1; i++) { //Для всех элементов массива in >> A[i]; //Считываем из файла } 

You can also use vectors ( std::vector<int> A; ) with appropriate methods (push_back, insert, etc.)

  • But how can I make it so that without an array? Because if I use an array, then the same will not work with the list and class, but will work with an array only. I need without an array like finding a way out. - navi1893
  • one
    Frankly, this is not the answer to this question. TC asks him how to name the list items (he means a linked list ). What does the array or vector? - avp
 //Самый простой способ: int arr[256]; for(int i = 0; i<=256; i++) cin >> arr[i];