C++;相当于python';s expandtabs()函数? 我需要实现ExcPATABSH()函数的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

C++;相当于python';s expandtabs()函数? 我需要实现ExcPATABSH()函数的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,python,c++,Python,C++,这就是我所拥有的: 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

这就是我所拥有的:

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;

std::string ExpandTabs(const std::string&str,int tabsize=4){
std::string ReturnString=str;
std::string result=“”;
int pos=0;
对于(std::string::size_type i=0;i
您需要一个字符一个字符地构建字符串。目前,您可以在函数的开头将
str
赋值给
ReturnString
,然后在字符串的末尾附加您认为必要的空格,而不是在制表符的位置

毫无疑问,有更多的惯用方法可以实现相同的结果,但是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';
}

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
}

为什么要添加到
ReturnString
?只需直接翻译python算法;不要添加新的变量之类的。您的不同之处是什么?输出结果是什么?(用大写字母命名变量不是标准的,但也没有错。)当我调用CUTE时,代码是什么?ExpTabes ?是不是用几个空格替换一个标签?然后就是 STD::Stult将在C++中完成。基本上是C++中的一条直线。非常感谢。这条线在我身边并不是很合适。现在好多了。
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
}