Home > Enterprise >  Is this a function or variable definition in c code?
Is this a function or variable definition in c code?

Time:11-20

I am reading a piece of c code in which ids, curi and len are integer, content are string. I don't understand what's match_word() part. Is it a function or variable? I can't find its definition in all header files.

 if(-1!=ids) 
          {
            len = ids - curi;
            string match_word(content, curi, len);
            bool rejudge = false;
            ...
          }

CodePudding user response:

From std::string documentation

string match_word(content, curi, len);

This is/uses a substring constructor which

Copies the portion of content that begins at the character position curi and spans len characters (or until the end of content, if either content is too short or if len is string::npos).

So for example

std::string s = "Hello World";
string match_word(s, 2, 7);
std::cout<<match_word<<std::endl; //prints llo Wor

The above will print llo Wor

Now coming to your question:

Is this a function or variable definition in c code?

Basically this is a variable definition using the substring constructor. So in your case match_word is a variable of type std::string.

CodePudding user response:

string match_word(content, curi, len);

match_word is a string. This is a declaration. Looks like you want to call this constructor:

string (const string& str, size_t pos, size_t len = npos);

Here is an example from cplusplus

#include <iostream>
#include <string>
int main()
{
    std::string s0 ("Initial string");
    std::string s3 (s0, 8, 3);
    std::cout << s3;
}

This will print:

str
  •  Tags:  
  • c
  • Related