how to get length of string in vector?

I cannot find out how to get lengh of the string which is referred by (*it). sizeof((*it)) or length did not work for me. The conversion to c string failed...

1
2
3
4
5
std::vector<std::string> raw_args( argv+1, argv+argc ) ;
std::vector<std::string>::iterator it = raw_args.begin();

char * pom = (*it).c_str();
int length = sizeof(pom);
Last edited on
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<string> Str;
    Str.push_back("Hello World");
    string *it = &Str[0];

    string str = it->c_str();//Copy string to string

    int length = it->size();
    const char *pom = it->c_str();//Point this(char*) to vectors string

    int cSize = it->size();
    char *cString = new char[cSize+1];//Ptr: Create new string, size it+1.
    for(int i=0;i<cSize;++i)
        cString[i] = it->c_str()[i];//Copy string by characters
    cString[cSize]='\0';//Add terminator to cString.

    cout<<str<<' '<<str.size()<<'\n';
    cout<<pom<<' '<<length<<'\n';
    cout<<cString<<' '<<cSize<<'\n';


    cout<<"\nPress <enter> to exit console: ";
    cin.get();
    return 0;
}
Sorry, I was not exact enough.
it definition:

1
2
std::vector<std::string> raw_args( argv+1, argv+argc ) ;
std::vector<std::string>::iterator it = raw_args.begin();

I use (*it) to return actual string during parsing of command line arguments
Last edited on
Is it necessary to convert it to a C-string? If not, I'd just use std::string's length() method.
Now it works,
int length = (*it).length();
I already tried before but I had to have mistake somewhere because was not working.
Last edited on
Topic archived. No new replies allowed.