Are elements in a vector stored contiguous in memory What if they are string elements? 20

Updated on technology 2024-03-03
12 answers
  1. Anonymous users2024-02-06

    The elements in the vector are indeed stored contiguous in memory. The implementation of vector is made up of a dynamic array. When space is insufficient, use a realloc function similar to C.

    Reallocate space. Because the elements in the vector are stored continuously, the vector supports random access to the elements in a constant time. The Iterator in the vector belongs to the Random Access Iterator

    If the vector stores the string type. The situation is the same. It's important to note that the string type is not a simple char array.

    Arrays in string are also dynamically allocated. For the sake of convenience, you can understand (not exactly) that when a vector stores a string type, there are actually many char* pointers in the array, and each pointer points to the content of the string character. can give you a simple diagram.

    vector |

    v v v v

    s1 s2 s3 s4

    As for what you mean by casting into memory, I don't quite understand what you're trying to mean, so I don't know how to talk about that.

  2. Anonymous users2024-02-05

    It's not appropriate... Friend. There's no point in forcing a container to copy, you still don't get continuous data. And, do you think the string space is continuous?

    The content of the string is also dynamically allocated, not only meaningless, but it is wrong to do so.

  3. Anonymous users2024-02-04

    No, if there is a lot of data, it will be divided into several pieces of memory.

    And string is a class, and cannot be used as a memory copy.

  4. Anonymous users2024-02-03

    Yes, no problem at all. The main problems with your program are:

    1.If there are templates inside the template, the two and "must be separated, otherwise they will be mistaken as shift operators and error." It's an easy mistake to make.

    All similar vectors> n,d; The statement is uniformly changed to vector< vector > n,d;

    2.You don't add a scope identifier when you define the send function outside of the class.

    You have emptied m, and an empty template cannot be entered.

    If the vector stores the string type. The situation is the same. It's important to note that the string type is not a simple char array. Arrays in string are also dynamically allocated.

    My answer is available for your reference, thank you.

  5. Anonymous users2024-02-02

    You just assign a value. This is the referencing of the address. The real content assignment is;

  6. Anonymous users2024-02-01

    vectorsp;

    string str;

    vector defines sp with string*; And the data you want to store is of type string ; Type mismatch (change vectorsp to vectorsp).

    In C++, std::string is generally in the form of string instead of string*

  7. Anonymous users2024-01-31

    Local variables are allocated on the stack, and the stack pointer before the function call should be the same as the stack pointer after the function returns, otherwise there is an error.

    How to assign you can write a small test program yourself.

    void test(void)

    char i,a[10];

    printf("0x%x", &i);

    printf("0x%x", a);

    printf("0x%x", a+1);

    printf("0x%x", a+2);

    printf("0x%x", a+3);

    There may also be alignment issues, and you may see that the assigned addresses are not contiguous.

    void test(void)

    char i;

    int j;

    printf("0x%x", &i);

    printf("0x%x", &j);

    If you see that the address is not continuous, for example, if the 4 bytes are aligned, i occupies one byte, and there are 3 bytes left, and j needs 4 bytes, and the remaining 3 bytes cannot accommodate j, and the remaining 3 bytes will not be used, then the addresses of j and i will not be continuous, and there will be a difference of 3 bytes. If there is no address alignment, it should be distributed on the stack continuously.

  8. Anonymous users2024-01-30

    Theoretically, any data type can be stored in the vector, no matter what type you add to it, it will return you with object.

    We can also display the data type in the specified vector, called generics, e.g. vectorv = new vector();

    After that, when you can only pass in int or integer type get() in the add() method, the int type will be automatically returned.

  9. Anonymous users2024-01-29

    vector In the case of vector without using a paradigm, both the add method and the get method are object types.

    It's an object type, you can't add it to int, you can use a cast to meet your needs:

    sum += (integer) ;

  10. Anonymous users2024-01-28

    sum += ;Why is this wrong?

    The error is reported because it is an object

    The default += operation is not supported, you should do this:

    sum += ((integer);Why is this wrong?

  11. Anonymous users2024-01-27

    placed inside the stack; Actually, it's a copy of the tmp. If you don't have a bitwise copy of the write copy constructor, which is not safe, if you write a copy constructor, you will copy it the same way you do.

  12. Anonymous users2024-01-26

    The elements of the vector are stored continuously, and as long as you get the pointer of any element, you can use the pointer to access each element in the vector, but if you insert or delete elements in the vector, this address may be invalid.

    A dereference iterator can get a reference to the element that the iterator points to, and you can use the address operator on this reference to get the memory address of that element.

Related questions
11 answers2024-03-03

In C++, vector is used as a container, and its function is to store variables of struct type. Here's how vector might be used: >>>More

8 answers2024-03-03

Vowel phonemes exist in a certain word, to put it simply, if there is a sound in the phonetic transcription of a certain word that is a vowel, then that word has a vowel phoneme! That's what it means! Vowel letters: a e i o u >>>More

9 answers2024-03-03

The midyear festival is the 15th day of the 7th month of the lunar calendar, and this year it is August 12th. >>>More

9 answers2024-03-03

The festival is also known as the "Ghost Festival", and it is said that this day is the day when the hell is opened, and the deceased ancestors can go home for reunion, so it is also a ghost festival.

10 answers2024-03-03

It is almost the midyear festival of July and a half, as a traditional festival in our country, it is based on the theme of Fengqin and Pudu, it is a festival where people place their ancestors on lovesickness, and it is also known as the "ghost festival" in the folk.