添加边框

时间:2016-06-03 21:24:02

标签: c++ border

我试图完成我的边界。目前它看起来像这样......

       ************************************
       *Body and Soul
       *Tenor Madness
       *Impressions
       *Lester Leaps In
       *There Will Never Be Another You
       *Summertime
       *Blues In The closet
       *Maiden Voyage
       ***********************************

如何获得边界的右侧?假设句子永远不会超过"永远不会有另一个你的#34;。

 void print(string list_array[]){

    for(int i=0; i<40; i++){
        cout << '*';
    }

    cout << endl;
    for(int i=0; i<20; i++){
        cout<<'*' <<list_array[i] << endl;
   }
    for(int i=0; i<40; i++){
        cout <<'*';
   }
    cout << endl;

}

1 个答案:

答案 0 :(得分:1)

使用std::setw或者您需要填充空格。

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;
void print( std::vector<std::string> &stringVec ) {
    cout << std::setfill( '*' ) << setw( 40 ) << '*' <<  endl;
    for( int i=0; i< stringVec.size(); i++ ) {
        cout << '*' << std::left << setfill(' ' )<< setw( 40 - 2 ) << stringVec[i] << '*'<< endl;
    }
    cout << std::setfill( '*' ) << setw( 40 ) << '*' <<  endl;
}

int main( )
{
    std::vector<std::string> vec = { "Body and Soul",
        "Tenor Madness",
        "Impressions",
        "Lester Leaps In",
        "There Will Never Be Another You",
        "Summertime" "Blues In The closet",
        "Maiden Voyage" };
    print( vec );
}

输出:

****************************************
*Body and Soul                         *
*Tenor Madness                         *
*Impressions                           *
*Lester Leaps In                       *
*There Will Never Be Another You       *
*SummertimeBlues In The closet         *
*Maiden Voyage                         *
****************************************