Home c++ The length of the C++ vector is not defined

The length of the C++ vector is not defined [Duplicate]

Author

Date

Category

Trying to determine the length of the transmitted vector, but always the length is defined as 8

# include & lt; iostream & gt;
#Include & lt; vector & gt;
#Include & lt; Cstddef & gt;
Using Namespace STD;
Class My String {
  Private:
  Vector & lt; Char & GT; STR;
  Public:
  MyString (Const Char X []) {
    Vector & lt; Char & GT; TEMPSTR (X, X + SIZEOF (X) / SIZEOF (* x));
    STR = TEMPSTR;
  };
  Void Print () {
    For (int i = 0; i & lt; str.size (); i ++) {
      COUT & LT; & LT; STR [i];
    };
    COUT & LT; & LT; "" & lt; & lt; endl;
  };
  Void Find (Const Char C []) {
    Vector & lt; Char & GT; TEMP (C, C + SIZEOF (C) / SIZEOF (* C));
    COUT & LT; & lt; temp.size () & lt; & lt; endl;
  };
};
INT MAIN ()
{
  MyString Str1 ("Dasdasda");
  STR1.PRINT ();
  STR1.FIND ("ASD");
  STR1.FIND ("DASDA");
  Return 0;
}

Answer 1, Authority 100%

[Sighing with a monotonous voice] As you know when transmitted to the function of the array (why do you call it a vector?!) It is converted into a pointer, so

sizeof (x) / sizeof (* x)

is

sizeof (char *) / Sizeof (Char)

or for 64-bit applications, 8/1 = 8.

What was asked – then the compiler and answered.

If you transmit strings, it would be more logical to work with rows. But, in the end, it is possible and

mystering (const char x []): str (x, x + strlen (x) +1) {}

+1 – in case you and the zero final symbol is needed.

But it would be better for you to take the string line, and not vector & lt; char & gt; – much more natural and convenient.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions