用C ++协调系统

时间:2012-10-09 11:53:59

标签: c++ arrays vector c++11 stdvector

您好我想用c ++制作一个坐标系。我将从用户那里获得一些(x,y)坐标并使用它我需要制作一个坐标系(更多的是地图)样式。我怎样才能做到这一点?它需要如下图所示。我应该使用2D数组或矢量吗?如何使循环以不同方式进行标记?

(2,0)(4,3)(7,8) 需要看起来像

 **1************
 ***************
 ***************
 ***************
 ***1***********
 ***************
 ***************
 ********1******

这是我到目前为止所获得的代码,但问题是我无法在其中标记多个坐标。我只是用2个循环来做它

for(int i = -6; i < 7; i++) 
    if (i < 0) 
        cout<<" "<<i; 
    else 
        cout<<"  "<<i; 
cout<<endl; 

for(int i = 0; i < 15; i++) 
    { 
        cout<<(char)(i + 49); 
        for(int j = -6; j < 7; j++) 
        if(i == y - 1 && j == x) 
            cout<<" x "; 
        else 
            cout<<" . "; 

        cout<<(char)(i + 49)<<endl; 
    } 

请指教。谢谢!!

3 个答案:

答案 0 :(得分:5)

我建议您使用vector<string>vector<vector<char> >甚至vector<vector<string> >,具体取决于您打算在单元格中存储的内容。如果单元格是单个字符,那么第一个选项可能是最好的。 之后创建地图非常简单:

int n,m;
cin >> n >> m;
vector<string> a(n, string(m, '*');

我不确定''是什么'。以及上面代码中的'x',但我为你留下的所有成像是输入几对坐标,并将vector<string>中的相应元素替换为'1'。

希望这有帮助。

答案 1 :(得分:1)

我建议使用std::set std::pair代替std::vector - 没有必要将整个网格保留在内存中,我们只需要点。

http://liveworkspace.org/code/f434521b804485f16786556762780448

答案 2 :(得分:0)

要回答您的其他问题,您可以使用循环进行更改,使用另一个循环显示结果。 使用izomorphius的建议,如果你使用一个列表来存储坐标对,它会是这样的:

vector<string> matrix ;
list<pair> PairList ;

for (list<pair>::const_iterator it = PairList.beguin(); i < PairList.end(); it++) {
    matrix[ (*it).second ][ (*it).first ] = "." ;
}

并显示结果:

for (int i = 0; i < matrix.size; i++) {
    cout << matrix[i] << endl ;
}
相关问题