二维数组,二维vector的声明初始化,&传递(iter,模板,C99特性),iterator遍历

来源:互联网 发布:摇钱树网吧计费软件 编辑:程序博客网 时间:2024/06/05 17:35


/*
program: iterator pass str
compile: g++ main.cpp
*/

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

void pass_str(const vector<vector<string> >::const_iterator &it_begin, 
     const vector<vector<string> >::const_iterator &it_end)

vector<vector<string> >::const_iterator it_i = it_begin;//第一维vector范围
vector<vector<string> >::const_iterator it_i_end = it_end;
for(int i=0; it_i != it_i_end; ++it_i){
   vector<string>::const_iterator it_j = (*it_i).begin();//第二维vector范围
   vector<string>::const_iterator it_j_end = (*it_i).end();
   for (int j=0; it_j != it_j_end; ++it_j){
    cout << *it_j << "   ";
   }
   cout << endl;
}
}
main()
{
string ia[12][12];
stringstream i_strm;
stringstream j_strm;

for(int i=0; i<12; ++i)//二维数组初始化
{
   i_strm.clear();//重置流的状态标志
   i_strm.str("");//清空流内容
   i_strm << i;
   for(int j=0; j<12; ++j){
    j_strm.clear();
    j_strm.str("");
    j_strm << j;
    ia[i][j]=i_strm.str()+" "+j_strm.str();
   }
}
//二维vector初始化
vector<vector<string> > vec_str(12, vector<string>(12));
for(i=0; i < vec_str.size(); i++){
   for(int j=0; j < vec_str[i].size(); j++){
    vec_str[i][j]=ia[i][j];
    }
}
vector<vector<string> >::const_iterator it_begin = vec_str.begin();
vector<vector<string> >::const_iterator it_end = vec_str.end();
pass_str(it_begin, it_end);

return 0;
}

/*
program: template pass str
compile: g++ main.cpp
注意,在gnu编译器下通过,而vc编译器下不能通过
*/

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

template <typename T, size_t M, size_t N>
void pass_str(const T (& str)[M][N]) {
    cout << "\nthis is the output of template function:" << endl;
    for (size_t i = 0; i < M; ++i) {
        for(size_t j=0; j< N; ++j) {
            cout << str[i][j] << "   ";
        }
        cout << endl;
    }
}

int main()
{
string ia[12][12];
stringstream i_strm;
stringstream j_strm;

for(int i=0; i<12; ++i)//二维数组初始化
{
   i_strm.clear();//重置流的状态标志
   i_strm.str("");//清空流内容
   i_strm << i;
   for(int j=0; j<12; ++j){
    j_strm.clear();
    j_strm.str("");
    j_strm << j;
    ia[i][j]=i_strm.str()+" "+j_strm.str();
   }
}
pass_str(ia);
return 0;
}

/*
program: C99 feature pass str
compile: gcc main.c -o main -std=c99
by nihui
*/

#include <stdio.h>
#include <string.h>

void pass_str1(int m, int n, char* str[m][n])
{
printf( "this is the function & output:\n" );

for(int i=0; i<m; ++i)
{
       for(int j=0; j<n; ++j){
         if(j==0){
            printf( "\n" );
         }
         printf( "%s   ", str[i][j] );
       }
}
}

int main()
{
const char name[] = "name";
char x[] = "0 0";
char y[] = "11 11";
char z[] = "000";

char* ia[12][12] = {0};

for(int k=0; k<12; ++k)
{
       ia[0][k]=x;
}
for(int k=0; k<12; ++k)
{
       ia[11][k]=y;
}
for(int i=1; i<11; ++i)
{
       for(int j=1; j<11; ++j)
         ia[i][j]=z;
}

pass_str1( 12, 12, ia );

return 0;
}

0 0
原创粉丝点击