简体   繁体   English

char* 到 std::string 的 Substring

[英]Substring of char* to std::string

I have an array of char s and I need to extract subsets of this array and store them in std::string s.我有一个char数组,我需要提取这个数组的子集并将它们存储在std::string中。 I am trying to split the array into lines, based on finding the \n character.我正在尝试根据查找\n字符将数组拆分为行。 What is the best way to approach this?解决这个问题的最佳方法是什么?

int size = 4096;
char* buffer = new char[size];
// ...Array gets filled
std::string line;
// Find the chars up to the next newline, and store them in "line"
ProcessLine(line);

Probably need some kind of interface like this:可能需要这样的接口:

std::string line = GetSubstring(char* src, int begin, int end);

I'd create the std::string as the first step, as splitting the result will be far easier.我会创建std::string作为第一步,因为拆分结果会容易得多。

int size = 4096;
char* buffer = new char[size];
// ... Array gets filled
// make sure it's null-terminated
std::string lines(buffer);

// Tokenize on '\n' and process individually
std::istringstream split(lines);
for (std::string line; std::getline(split, line, '\n'); ) {
   ProcessLine(line);
}

You can use the std::string(const char *s, size_t n) constructor to build a std::string from the substring of a C string.您可以使用std::string(const char *s, size_t n)构造函数从 C 字符串的 substring 构建std::string string。 The pointer you pass in can be to the middle of the C string;你传入的指针可以指向C字符串的中间; it doesn't need to be to the very first character.它不需要是第一个字符。

If you need more than that, please update your question to detail exactly where your stumbling block is.如果您需要更多,请更新您的问题以详细说明您的绊脚石在哪里。

your best bet (best meaning easiest) is using strtok and convert the tokens to std::string via the constructor.您最好的选择(最好的意思是最简单的)是使用strtok并通过构造函数将标记转换为std::string (just note that pure strtok is not reentrant, for that you need to use the non standard strtok_r ). (请注意,纯strtok不可重入,因为您需要使用非标准strtok_r )。

void ProcessTextBlock(char* str)
{
    std::vector<std::string> v;
    char* tok = strtok(str,"\n");
    while(tok != NULL)
    {
        ProcessLine(std::string(tok));
        tok = strtok(tok,"\n");
    }
}

I didn't realize you only wanted to process each line one at a time, but just in case you need all the lines at once, you can also do this:我没有意识到您只想一次处理每一行,但以防您一次需要所有行,您也可以这样做:

std::vector<std::string> lines;

char *s = buffer;
char *head = s;
while (*s) { 
  if (*s == '\n') { // Line break found
    *s = '\0'; // Change it to a null character
    lines.push_back(head); // Add this line to our vector
    head = ++s;
  } else s++; // 
}
lines.push_back(head); // Add the last line

std::vector<std::string>::iterator it;
for (it = lines.begin(); it != lines.end(); it++) {
  // You can process each line here if you want
  ProcessLine(*it);
}
// Or you can process all the lines in a separate function:
ProcessLines(lines);

// Cleanup
lines.erase(lines.begin(), lines.end());

I've modified the buffer in place, and the vector.push_back() method generates std::string objects from each of the resulting C substrings automatically.我已经修改了缓冲区,并且 vector.push_back() 方法自动从每个生成的 C 子字符串生成 std::string 对象。

You can turn a substring of char* to std::string with a std::string's constructor:您可以使用 std::string 的构造函数将 char* 的 substring 转换为 std::string:

template< class InputIterator >
basic_string( InputIterator first, InputIterator last, const Allocator& alloc = Allocator() );

Just do something like:只需执行以下操作:

char *cstr = "abcd";
std::string str(cstr + 1, cstr + 3);

In that case str would be "bc".在这种情况下,str 将是“bc”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM