c++语法随笔

函数原型
const char *c_str();
  • c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同。
  • 这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
  • 注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针。
    比如:最好不要这样:
1 char* c;
2 string s="1234";
3 c = s.c_str();
  • c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将一个const char *赋与一个char *
  • 应该这样用:
1 char c[20];
2 string s="1234";
3 strcpy(c,s.c_str());
  • 这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作。
判断map中是否存在某个键时
1 .使用find()函数
if (mymap.find(key) == mymap.end())
    cout << "没有这个key" << endl;
2. 使用count()函数
if (mymap.count(key) == 0)
    cout << "no this key" << endl;
multiple的使用

multimap 能接受一key对应多value的情况
multimap 会自动按key值排序,而value则按输入顺序排序,要想把一个key对应的多个value排序
必须自己构造排序函数

typedef pair<string, string> PAIR;
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) {
    if(lhs.first==rhs.first)
        return lhs.second<rhs.second;
    else return lhs.first<rhs.first;

对multimap中的元素访问。返回键对应的所有值。

  • 通过count()函数统计键所对应的值的个数
  • 通过find()函数,找到地址,通过迭代器依次访问每个元素
void test(){
    multimap<int,int> test_map;
    test_map.insert(pair<int,int>(1,2));
    test_map.insert(pair<int,int>(1,3));
    test_map.insert(pair<int,int>(1,4));
    test_map.insert(pair<int,int>(2,4));
    test_map.insert(pair<int,int>(2,7));
    int num=test_map.count(1);
    multimap<int,int>::iterator iter;
    iter=test_map.find(1);
    while(num){
        cout<<(*iter).second<<endl;
        iter++;
        num--;
    }
    exit(0);
}

即能访问到1对应的所有元素。

multimap的第二种访问方式
   multimap<string, string> mulMap;
    mulMap.insert(make_pair("鲁迅", "朝花夕拾"));
    mulMap.insert(make_pair("鲁迅", "阿Q正传"));
    mulMap.insert(make_pair("鲁迅", "野草"));
    mulMap.insert(make_pair("罗贯中", "三国演义"));
    mulMap.insert(make_pair("罗贯中", "隋唐志传"));
    mulMap.insert(make_pair("琼瑶", "还珠格格"));
    mulMap.insert(make_pair("琼瑶", "情深深雨蒙蒙"));
    typedef multimap<string, string>::iterator multiMapItor;
 
    
    string author("鲁迅");
    cout << author << "的书籍有:" << endl;
    pair<multiMapItor, multiMapItor> pos = mulMap.equal_range(author);
    while(pos.first != pos.second)
    {
        cout << pos.first->second << endl;
        ++pos.first;
    }
    cout << endl;

[1]C string 对比函数strcmp()
  • 函数原型:int strcmp ( const char * str1, const char * str2 );
  • 此函数开始比较每个字符串的第一个字符。 如果它们彼此相等,则继续比较下一个字符,直到字符不同或直到达到终止空字符。(根据ASCII码进行比较)
  • 如果返回结果为0 ,说明两个字符串相等。
  • 返回结果>0,则表明第二字C string 在第一个不相等字符大于第一个C string
  • <0,则相反
[2]std::string 字符串对比函数compare()
  • 函数原型(c++ 11标准)
string (1)	

int compare (const string& str) const noexcept;

substrings (2)	

int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str,
             size_t subpos, size_t sublen) const;

c-string (3)	

int compare (const char* s) const;
int compare (size_t pos, size_t len, const char* s) const;

buffer (4)	

int compare (size_t pos, size_t len, const char* s, size_t n) const;

  • 该字符串通过调用compare函数,与传进compare参数的字符串进行比较。

  • 参数 str 表示另一个字符串对象,完全(或部分)用作比较字符串。

  • 参数 pos 要被比较字符串(即调用compare()的字符串)的起始位置

  • 参数 len 比较的长度

  • 参数 subpos, sublen 对参数字符串而言

  • 返回结果等于0,则相等。

  • 小于0,第二个大

  • 大于0,第一个大。

// comparing apples with apples
#include <iostream>
#include <string>

int main ()
{
  std::string str1 ("green apple");
  std::string str2 ("red apple");

  if (str1.compare(str2) != 0)
    std::cout << str1 << " is not " << str2 << '\n';

  if (str1.compare(6,5,"apple") == 0)
    std::cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    std::cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    std::cout << "therefore, both are apples\n";

  return 0;
}
Output:

green apple is not red apple
still, green apple is an apple
and red apple is also an apple
therefore, both are apples

猜你喜欢

转载自blog.csdn.net/liujiang0529/article/details/84232786