zy_sunshine 发表于 2009-12-8 14:12:38

继续引申,下面是模板的实现方式,但是这里的 模板也太强大了吧?template <typename T, size_t M, size_t N>
void print(const T (& r)) {
    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 << r << "   ";
      }
      cout << endl;
    }
}
在vc,也就是微软的编译器下编译不能通过,gnu编译器下正常。不知道是不是没有C++特性的问题,看样要升级一下VC了。
D:\Program Files\Microsoft Visual Studio\MyProjects\give_string\give_string.cpp(28) : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
D:\Program Files\Microsoft Visual Studio\MyProjects\give_string\give_string.cpp(28) : error C2087: '<Unknown>' : missing subscript

[ 本帖最后由 zy_sunshine 于 2010-3-12 20:53 编辑 ]

nihui 发表于 2009-12-8 18:12:11

c++ 编译器不允许 c++ 语言使用 c99 标准.... 8O

gcc main.c -o main -std=c99#include <stdio.h>
#include <string.h>

void pass_str1(int m, int n, char* str)
{
    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 );
      }
    }
}

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

    char* ia = {0};

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

    pass_str1( 12, 12, ia );

    return 0;
}

nihui 发表于 2009-12-8 18:20:49

模板非常强大,c++ 比较困难的东西.....:wink:

zy_sunshine 发表于 2009-12-8 21:50:51

回复 17# nihui 的帖子

yeah ,nihui写的很漂亮哈

zy_sunshine 发表于 2009-12-8 22:21:09

原来还有C有而C++没有的,这个C99标准我一直是理解错的,以为是C++的

haulm 发表于 2009-12-9 00:06:02

还是认为不应该用string数组来管理字符串列表。

[ 本帖最后由 haulm 于 2009-12-9 00:10 编辑 ]

zy_sunshine 发表于 2009-12-9 00:29:51

感觉用string管理字符串列表更C++化

我用vector做了一下,出现问题,二维vector的iterator没法遍历:cry:#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

//二维vector的 iterator不知道怎么遍历了
void pass_str4(const vector<vector<string> >::iterator &it_begin, const vector<vector<string> >::iterator &it_end)
{
        cout << endl;
        cout << "this is the function vector output:" << endl;

        vector<vector<string> >::iterator it = it_begin;
        for(int i=0; it != it_end; ++it, ++i){
                for (int j=0; j < it.size(); ++j){
                        cout << it << "   ";
                }
                cout << endl;
        }
}

int main(int argc, char* argv[])
{
        string ia;
        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_strm.str()+" "+j_strm.str();
          }
        }


        vector<vector<string> > vec_str(12, vector<string>(12));
        for(i=0; i < vec_str.size(); i++){
                for(int j=0; j < vec_str.size(); j++){
                        vec_str=ia;
                }
        }

pass_str4(vec_str.begin(), vec_str.end());



        for(int i=0; i < 12; ++i){                //输出二维vector
      for(int j=0; j < 12; ++j){
            cout << vec_str << "   ";
      }
      cout << endl;
    }

        return 0;
}
结果:0 0   0 1   0 2   0 3   0 4   0 5   0 6   0 7   0 8   0 9   0 10   0 11
2 0   2 1   2 2   2 3   2 4   2 5   2 6   2 7   2 8   2 9   2 10   2 11
4 0   4 1   4 2   4 3   4 4   4 5   4 6   4 7   4 8   4 9   4 10   4 11
6 0   6 1   6 2   6 3   6 4   6 5   6 6   6 7   6 8   6 9   6 10   6 11
8 0   8 1   8 2   8 3   8 4   8 5   8 6   8 7   8 8   8 9   8 10   8 11
10 0   10 1   10 2   10 3   10 4   10 5   10 6   10 7   10 8   10 9   10 10   10 11看样是对iterator的理解不到位了

[ 本帖最后由 zy_sunshine 于 2009-12-9 00:37 编辑 ]

zy_sunshine 发表于 2009-12-9 00:34:00

halm给个管理二维string的实例看看?:)

nihui 发表于 2009-12-9 08:25:53

:wink:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

void pass_str4(const vector< vector<string> >::const_iterator &it_begin, const vector< vector<string> >::const_iterator &it_end)
{
    cout << "this is the function vector output:" << endl;
   
    vector<vector<string> >::const_iterator it1 = it_begin;
    vector<vector<string> >::const_iterator it1_end = it_end;
    for( ; it1 != it1_end; ++it1 ) {
      // it1 is an iterator, *it1 is the vector<string> of vector< vector<string> >
      vector<string>::const_iterator it2 = (*it1).begin();
      vector<string>::const_iterator it2_end = (*it1).end();
      for ( ; it2 != it2_end; ++it2 ) {
            // it2 is an iterator, *it2 is the string of vector<string>
            cout << *it2 << '\t';
      }
      cout << endl;
    }
}

int main(int argc, char* argv[])
{
    string ia;
    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_strm.str()+" "+j_strm.str();
      }
    }
   
    // represent array as vector
    vector< vector<string> > vec_str(12, vector<string>(12));
    for(int i=0; i < vec_str.size(); i++){
      for(int j=0; j < vec_str.size(); j++){
            vec_str=ia;
      }
    }
   
    pass_str4(vec_str.begin(), vec_str.end());
   
    // print vector content
    for(int i=0; i < 12; ++i){
      for(int j=0; j < 12; ++j){
            cout << vec_str << '\t';
      }
      cout << endl;
    }
   
    return 0;
}

zy_sunshine 发表于 2009-12-9 09:22:16

回复 24# nihui 的帖子

感激涕零,回去看书写代码了......:P
:shock:

haulm 发表于 2009-12-10 12:12:49

学习了。

haulm 发表于 2009-12-10 13:11:11

原帖由 zy_sunshine 于 2009-12-9 09:22 发表 http://www.linuxfans.org/bbs/images/common/back.gif
感激涕零,回去看书写代码了......:P
:shock:
我把你的代码整理了一下,要不我看得头晕目炫。
函数体里没分配内存时运行打印非常慢,所以pass_str3的方式值得推敲。
疑问是,如果我用的不是string数组,而是char数组的话,是否也存在这种问题呢。#include <iostream>
#include <string>
using namespace std;
void pass_str1(const string str)//这个值传的
{
      cout << endl;
      cout << "this is the function output:" << endl;
      
      for(int i=0; i<12; ++i)
      {
                for(int j=0; j<12; ++j){
                        if(j==0){
                              cout << endl;
                        }
                        cout << str << "   " ;
                }
      }
}

void pass_str2(const string* str)       //这个传的首地址,但是发现,如果string数组元素没有赋值是不会分配内存的,输出上有点小麻烦。
{
      cout << *(str+12*12-1) << endl;
}

void pass_str3(int m, int n, string (& str))
{
      cout << endl;
      cout << "this is the function & output:" << endl;

      for(int i=0; i<m; ++i)
      {
                for(int j=0; j<n; ++j){
                        if(j==0){
                              cout << endl;
                        }
                        cout << str << "   " ;
                }
      }
}

int main(int argc, char* argv[])
{
      string name("name");
      string ia;
      for(int k=0; k<12; ++k)
      {
                for(int s=0; s<12; ++s){
                ia="abcd";
      }
}
      cout<<ia;
        pass_str1(ia);
        pass_str1(&ia);
        pass_str2(ia);
        pass_str2(&ia);
        pass_str3(10,12,ia);
         return 0;
}

[ 本帖最后由 haulm 于 2009-12-10 13:41 编辑 ]

haulm 发表于 2009-12-10 13:47:07

nihui 的代码 void pass_str3(int m, int n, string (& str))
这样的写法是不被允许的                                 
a.cpp:4: 错误:数组边界不是一个整数常量

zy_sunshine 发表于 2009-12-10 19:01:07

回复 27# haulm 的帖子

传值这东西如果 调用频率不想inline这么频繁的话   应该没什么问题。

传值过去的时候在堆栈中已经压入string数组的值,相当于已经分配内存了,在函数返回平衡堆栈的时候会清除这些传入变量。

进入函数前的堆栈
main(int 1, char * * 0x003812c0) line 52 + 12 bytes
mainCRTStartup() line 206 + 25 bytes
KERNEL32! 7c817077()

进入函数后的堆栈
pass_str1(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > * 0x0012f664) line 5
main(int 1, char * * 0x003812c0) line 52 + 12 bytes
mainCRTStartup() line 206 + 25 bytes
KERNEL32! 7c817077()

这个是在vc里面看到的堆栈,我猜的。。没有看反汇编的程序还不能确定(俺的汇编基本功也不行)。

nnd,明天考试,金融的,整本书都还没看呢,去自习了。
页: 1 [2]
查看完整版本: 一个C++编程问题 用&方式传递字符串数组