简体   繁体   English

C++ 相当于 python 的 expandtabs() function?

[英]C++ equivalent of python's expandtabs() function?

I need to implement the C++ equivalent of the expandtabs() function.我需要实现与 expandtabs() function 等效的 C++。 Can someone help me with converting this code to C++?有人可以帮我将此代码转换为 C++ 吗?

def expandtabs(string, n):
    result = ""
    pos = 0
    for char in string:
        if char == "\t":
            # instead of the tab character, append the
            # number of spaces to the next tab stop
            char = " " * (n - pos % n)
            pos = 0
        elif char == "\n":
            pos = 0
        else:
            pos += 1
        result += char
    return result

This is what I have:这就是我所拥有的:

std::string ExpandTabs(const std::string &str, int tabsize =4){

  std::string ReturnString = str;
  std::string result = " ";
  int pos = 0;

  for(std::string::size_type i = 0; i < ReturnString.size(); ++i) {
    if (ReturnString[i] == '\t'){
      int spaces = tabsize - pos % tabsize ;
      ReturnString.append(" ", spaces);
      pos = 0;
    }
    else{
      pos+=1;
    }

}
  return ReturnString;

You need to build up the string character by character.您需要逐个字符地构建字符串。 Currently you assign str to ReturnString at the start of the function and then append whatever spaces you decide are necessary to the end of the string, instead of in place of the tabs.目前,您在 function 的开头将str分配给ReturnString ,然后将 append 分配给您决定字符串末尾所需的任何空格,而不是代替制表符。

There are no doubt more idiomatic ways to achieve the same result, but a like for like conversion of the python might look like.毫无疑问,有更多惯用的方法可以实现相同的结果,但 python 的类似转换可能看起来像。

#include <iostream>
#include <string>

std::string expand_tabs(const std::string &str, int tabsize=4)
{
    std::string result = "";
    int pos = 0;

    for(char c: str)
    {
        if(c == '\t')
        {
            // append the spaces here.
            result.append(tabsize - pos % tabsize, ' ');
            pos = 0;
        } else
        {
            result += c;
            pos = (c == '\n') ? 0: pos + 1;
        }         
    }

    return result;
}

int main()
{
    std::cout << expand_tabs("i\tam\ta\tstring\twith\ttabs") << '\n';
    std::cout << expand_tabs("1\t2\t3\t4", 2) << '\n';
}

It basically steps through the input forwarding on any non tab characters to the result string, otherwise it adds the correct number of spaces to the result.它基本上逐步将任何非制表符字符的输入转发到结果字符串,否则它会在结果中添加正确数量的空格。

Output: Output:

i   am  a   string  with    tabs
1 2 3 4

A straight translation of the python code is problematic, since char cannot be both a string and a single character, but apart from that it's straightforward: python 代码的直接翻译是有问题的,因为char不能既是字符串又是单个字符,但除此之外它很简单:

std::string expandtabs(std::string const&str, std::string::size_type tabsize=8)
{
    std::string result;
    std::string::size_type pos = 0
    result.reserve(str.size());  // avoid re-allocations in case there are no tabs
    for(c : str)
        switch(c) {
        default:
            result += c;
            ++pos;
            break;
        case '\n':
            result += c;
            pos = 0;
            break;
        case '\t':
            result.append(tabsize - pos % tabsize,' ');
            pos = 0;
        }
    return result
}

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

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