Thứ Tư, 20 tháng 4, 2016

Làm thế nào để đảo chuỗi nhanh trong c++

Có 2 cách:
- Cách 1: Tự viét hàm
// A Simple C++ program to reverse a string
#include<bits/stdc++.h>
using namespace std;

// Function to reverse a string
void reverseStr(string &str)
{
    int n = str.length();

    // Swap character starting from two
    // corners
    for (int i=0; i<n/2; i++)
       swap(str[i], str[n-i-1]);
}

// Driver program
int main()
{
   string str = "geeksforgeeks";
   reverseStr(str);
   cout << str;
   return 0;
}
- Cách 2: Dùng hàm có sẵn:
// A quickly written program for reversing a string
// using reverse()
#include<bits/stdc++.h>
using namespace std;
int main()
{
   string str = "geeksforgeeks";
   
   // Reverse str[beign..end]
   reverse(str.begin(),str.end());
   
   cout << str;
   return 0;
}

1 nhận xét:

  1. According to Stanford Medical, It's in fact the SINGLE reason women in this country get to live 10 years longer and weigh 42 pounds lighter than we do.

    (And really, it is not about genetics or some secret diet and absolutely EVERYTHING to do with "how" they eat.)

    P.S, I said "HOW", not "what"...

    TAP this link to see if this little quiz can help you find out your true weight loss potential

    Trả lờiXóa

Copyright © Dịch từ nguồn geeksforgeeks.org

Distributed By My Blogger Themes | Blogger Theme By NewBloggerThemes Up ↑