String array length printed instead of the Number of Array Items in C++

user2410243

I tried this declaration of array and check the size:

string path1[1] = {"D:\\Users\\user-pc\\Desktop\\testing\\inputs\\xml_source_example.xml"};
cout << path1->length();

and checked the size:

62

I wanted the output to be 1, so I tried with ->size() and I still got 62. I know that the first item in the array has length of 62, but I want the number of items in the array.

How can I get how many items there are in the array?

juanchopanza

Try this, leveraging std::begin and std::end:

std::end(path1) - std::begin(path1);

Alternatively, you can role out your own array size function:

#include <cstddef> // for std::size_t

template <typename T, std::size_t N>
constexpr std::size_t size(const T(&)[N])
{
  return N;
}

Usage:

#include <iostream>

int main()
{
  int a[42];
  std::cout << size(a) << std::endl;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Array printed as a string in csv

From Dev

Populate limited number of items in array, but keep array length - mongoose

From Dev

C++ Length of a string in an Array

From Dev

Length of items by field in an array

From Dev

array in R number of items to replace is not a multiple of replacement length

From Dev

Order of array items changing when being printed

From Dev

Getting number of ITEMS in an array C#

From Dev

Ruby .join method returning length of original array instead of concatenated string

From Dev

In C, sort array of strings by string length

From Dev

c++ Pass an array instead of a variable length argument list

From Dev

Length of an Array of String in JavaScript

From Dev

String[] array length isnt equal to the actual number of elements inside it

From Dev

JSON Array instead of string

From Dev

Get an array instead of string

From Dev

byte array with variable length to number

From Dev

Array length Vs Number of values in Array

From Dev

Array with undefined length in C

From Dev

C Length of Array (filled)

From Dev

Length of a char array in C

From Dev

Count the Number of elements in a string Array c#

From Dev

Reduce array to set number of items?

From Dev

Getting array length of items with a specific attribute

From Dev

In an array, how can I get values printed the way I would like them printed instead of the last value?

From Dev

Why is it necessary to declare a string's length in an array of strings in C?

From Dev

C++ string length: Array elements vs glyphs

From Dev

Easy C++ = array length and reverse WITHOUT <string>

From Dev

Sort string array by element length

From Dev

Split string into array of n length

From Dev

Count length of each string in an array

Related Related

HotTag

Archive