找一缕阳光呀 2017-08-06 12:00 采纳率: 93.3%
浏览 767
已采纳

为什么一个代码不对?还是说只能像第二个代码那样转换传值

//第一个
#include
#include
#include
using namespace std;
int main()
{
int n,m ;
scanf("%d %d",&n,&m);
map s;
map p;
** string s1,s2;
for(int i=0;i {
cin>>s1>>s2;
s[s1]=s2;
s[s2]=s1;
}
vector v;
for(int i=0;i {
cin>>s1;
v.push_back(s1);
}
**
for(auto it=v.begin();it!=v.end();it++)
{
int mark=0;
if(s.find((*it))!=s.end())
{
cout<<m[(*it)]<<endl;
continue;
}
else
{
mark = 1;
}
if(p.find((*it))!=p.end())
{
cout<<p[(*it)]<<endl;
continue;
}
else
{
mark = 1;
}
if(mark)
cout<<"Null\n";
}
return 0;
}


//第二个
#include
#include
#include
using namespace std;
int main()
{
char s1[2000],s2[2000];
string str1,str2;
map map_key;
map map_value;
int n,m;
scanf("%d %d",&n,&m);
for(int i=0; i {
scanf("%s %s",s1,s2);
str1 = s1;
str2 = s2;
map_key[str1] = str2;
map_value[str2] = str1;
}
vector vec;
for(int i=0; i<m; ++i)
{
scanf("%s",s1);
str1 = s1;
vec.push_back(str1);
}
for(auto it=vec.begin(); it!=vec.end(); it++)
{
int mark = 0;
if(map_key.find((*it)) != map_key.end())
{
cout<<map_key[(*it)]<<endl;
continue;
}
else
{
mark = 1;
}
if(map_value.find((*it)) != map_value.end())
{
cout<<map_value[(*it)]<<endl;
continue;
}
else
{
mark = 1;
}
if(mark)
cout<<"Null\n";
}
return 0;
}


  • 写回答

1条回答 默认 最新

  • shen_wei 2017-08-07 08:46
    关注
     int main( )
    {
       using namespace std;   
       vector <int> v1;
    
       v1.push_back( 1 );
       if ( v1.size( ) != 0 )
          cout << "Last element: " << v1.back( ) << endl;
    
       v1.push_back( 2 );
       if ( v1.size( ) != 0 )
          cout << "New last element: " << v1.back( ) << endl;
    }
    
    
    int main( ) {
       using namespace std;
       map <int, int>::iterator m1_pIter, m2_pIter;
    
       map <int, int> m1, m2;
       typedef pair <int, int> Int_Pair;
    
       m1.insert ( Int_Pair ( 1, 10 ) );
       m1.insert ( Int_Pair ( 2, 20 ) );
       m1.insert ( Int_Pair ( 3, 30 ) );
       m1.insert ( Int_Pair ( 4, 40 ) );
    
       cout << "The original key values of m1 =";
       for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
          cout << " " << m1_pIter -> first;
       cout << "." << endl;
    
       cout << "The original mapped values of m1 =";
       for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
          cout << " " << m1_pIter -> second;
       cout << "." << endl;
    
       pair< map<int,int>::iterator, bool > pr;
       pr = m1.insert ( Int_Pair ( 1, 10 ) );
    
       if( pr.second == true ) {
          cout << "The element 10 was inserted in m1 successfully." << endl;
       }
       else {
          cout << "Key number 1 already exists in m1\n"
               << "with an associated value of ( (pr.first) -> second ) = " 
               << ( pr.first ) -> second 
               << "." << endl;
       }
    
       // The hint version of insert
       m1.insert( --m1.end( ), Int_Pair ( 5, 50 ) );
    
       cout << "After the insertions, the key values of m1 =";
       for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
          cout << " " << m1_pIter -> first;
       cout << "," << endl;
    
       cout << "and the mapped values of m1 =";
       for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
          cout << " " << m1_pIter -> second;
       cout << "." << endl;
    
       m2.insert ( Int_Pair ( 10, 100 ) );
    
       // The templatized version inserting a range
       m2.insert( ++m1.begin( ), --m1.end( ) );
    
       cout << "After the insertions, the key values of m2 =";
       for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
          cout << " " << m2_pIter -> first;
       cout << "," << endl;
    
       cout << "and the mapped values of m2 =";
       for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
          cout << " " << m2_pIter -> second;
       cout << "." << endl;
    }
    

    vector 和 map 对数据添加的方法

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘