繁体   English   中英

在打印矢量内容时添加空格和新行

[英]Adding spaces and new lines in printing the content of vector

一个小问题。 如何添加空格和新的线条符号,以使效果看起来像那样?

ACCT
 CCTG
  CTGA

等等

vector<string>::reverse_iterator it;
   for( it=vec.rbegin(); it!=vec.rend(); ++it )
   {

      cout<<*it;
      cout<<endl;
      cout<<" ";

   }

我试过这种方式,但转变只是在第一个元素之后,就像那样:

ACCT
 CCTG
CTGA

非常感谢您的帮助!

使用这个:

std::string space = "";

for(auto it = vec.rbegin(); it != vec.rend(); ++it ) {
    std::cout << space << *it << std::endl;
    space += " ";
}

这个不会在新行上输出最终空格。

vector<string>::reverse_iterator it;
   string space="";
   for( it=vec.rbegin(); it!=vec.rend(); ++it )
   {

      cout<<*it;
      cout<<endl;
      space += " ";
      cout<<space;

   }
size_t spaces = 0;
for (auto it = vec.rbegin(); it != vec.rend(); ++it)
    cout << string(spaces++, ' ') << *it << '\n';

暂无
暂无

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

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