首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问错在哪里,c++

2012-09-03 
请教错在哪里,c++#include iostream#include string.h///还想问下,这里写string为什么不可以using n

请教错在哪里,c++
#include <iostream>
#include <string.h>///还想问下,这里写<string>为什么不可以
using namespace std;

int main()
{
  string str1;
  string str2;
  const char *a,*b;
  while(true)
  {
  cin>>str1>>str2;
  //cout<<str1.length()<<endl;
  a=new char [str1.length()];
  b=new char [str2.length()];
  a=str1.c_str();
  b=str1.c_str();
  int len1=strlen(a);
  int len2=strlen(b);
  int i,j;
  int **dp=new int [len1+1][len2+1];//这句报错,cannot appear in a constant-expression
  dp[0][0]=0;
  for(i=1;i<len1;i++)
  for(j=1;j<len2;j++)
  if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;
  else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
  cout<<dp[len1][len2]<<endl;

  }
  return 0;
}


[解决办法]
不清楚LZ想实现什么功能....

C/C++ code
#include <iostream>#include <string>#include <cstring>#include <algorithm>using namespace std;int main(){    string str1;    string str2;    string jud;    do    {        cout << "Enter string1 and string2:" << endl;        cin >> str1 >> str2;        char *a = new char[str1.length() + 1];        char *b = new char[str2.length() + 1];        strncpy(a, str1.c_str(), str1.length());        a[str1.length()] = '\0';        strncpy(b, str2.c_str(), str2.length());        b[str2.length()] = '\0';        int len1 = strlen(a);        int len2 = strlen(b);        int **dp = new int*[len1 + 1];        for (int ix = 0; ix != len1 + 1; ++ix)        {             int *p = new int[len2 + 1];             dp[ix] = p;        }        int flag = 0;        for(int i = 0; i != len1 + 1; i++)        {            for(int j = 0; j < len2 + 1; j++)            {                if (a[i] == b[j])                    dp[i][j] = flag + 1;                else                    dp[i][j] = 0;            }        }        for(int i = 0; i != len1 + 1; i++)        {            for(int j = 0; j < len2 + 1; j++)            {                cout << dp[i][j] << " " ;            }            cout << endl;        }        delete [] a;        delete [] b;        for (int ix = 0; ix != len1 + 1; ++ix)        {             delete [] dp[ix];        }        delete [] dp;        cout << "END ? (y = yes, n = no) " << endl;        cin >> jud;    }while(jud[0] != 'n' && jud[0] != 'N');    return 0;}
[解决办法]
#include <string.h>///还想问下,这里写<string>为什么不可以

<string.h>是C头文件,内部包含一系列处理字符串的函数的声明,所处理的字符串是按照C语言规定的连续存储以'\0'结尾的字符集合
<string>是C++头文件,内部包含C++标准模板类string的声明和实现,string继承自标准模板vector,实现了自动内存管理和大多数字符串常用操作

看着很像?看着很像?看着很像?

热点排行