compare two strings in C++

compare()

int main() 
{ 
    string s1("StringA"); 
    string s2("StringBC"); 
    int x = s1.compare(s2); 
  
    if (x != 0) 
        cout << s1 << " is not equal to " << s2 << endl; 
    else if(x == 0)
        cout << s1 << " is equal to " << s2 << endl; 

    if (x > 0) 
        cout << s1 << " is greater than " << s2 << endl; 
    else
        cout << s2 << " is greater than " << s1 << endl;


    return 0; 
} 

operator ==

int main() 
{ 
    string s1("StringA"); 
    string s2("StringBC"); 
  
    if (s1 != s2) 
        cout << s1 << " is not equal to " << s2 << endl; 
    else if(s1 == s2)
        cout << s1 << " is equal to " << s2 << endl; 
    return 0; 
} 

Differences between compare() and operator==

  // operator ==
  /**
   *  @brief  Test equivalence of two strings.
   *  @param __lhs  First string.
   *  @param __rhs  Second string.
   *  @return  True if @a __lhs.compare(@a __rhs) == 0.  False otherwise.
   */
  template<typename _CharT, typename _Traits, typename _Alloc>
    inline bool
    operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
	       const basic_string<_CharT, _Traits, _Alloc>& __rhs)
    { return __lhs.compare(__rhs) == 0; }
  • string::operator==() is using string::compare()

Git refs/for

git push [option] [ < repository > [< refspec >]]

refs

  • git manages all commit in key-value form
  • Key is 40 digits of hash value made of SHA-1.
  • Hash values are stored in a file with an name References
    • .git/refs
  • All refs are stored in .git/refs
  • Heads, remotes, and tags directories exist at .git/refs
$ pwd
.git/refs

$ ls
heads/  remotes/  tags/

$ cd
heads/

$ ls
master

$ cat master
6aeawerdfgdfg861145f19sdfafetrgf80af91
  • in above example, Master Branch is refs
  • git refers to the refs pointing to a particular task as branch

refs/for in Gerrit

Gerrit Documentation

When pushing a new or updated commit to Gerrit, you push that commit using a reference, in the refs/for namespace. This reference must also define the target branch, such as refs/for/[BRANCH_NAME].

For example, to create a new change on the master branch, you would use the following command:

$ git push origin HEAD:refs/for/master

The refs/for/[BRANCH_NAME] syntax allows Gerrit to differentiate between commits that are pushed for review and commits that are pushed directly into the repository.

Merge Squash

Squash multiple commit logs into one commit

(master)$ git init
(master)$ git add .
(master)$ git commit -m 'initial file'
(master)$ git checkout -b feature

(feature)$ touch fileA
(feature)$ git add .
(feature)$ git commit -m 'feature file - A'

(feature)$ touch fileB
(feature)$ git add .
(feature)$ git commit -m 'feature file - B'

(feature)$ touch fileA
(feature)$ git add .
(feature)$ git commit -m 'feature file - C'

(feature)$ touch fileA
(feature)$ git add .
(feature)$ git commit -m 'feature file - D'

(feature)$ git log --oneline
4beaeec1 D
670fb04e C
93a666e1 B
d839f20a A
  • HEAD is currently pointing at D

Squash D,C,B into A

$ git rebase -i HEAD~3
  • An editor will be fired up with all the commits in your current branch (ignoring merge commits), which come after the given commit. You can reorder the commits in this list to your heart’s content, and you can remove them. The list looks more or less like this:
pick d839f20a A
pick 93a666e1 B
pick 670fb04e C
pick 4beaeec1 D
...
  • Update the file as below by refering option commands
r d839f20a A Changed
s 93a666e1 B
s 670fb04e C
s 4beaeec1 D
...
  • Commands:
    • p, pick : use commit
    • r, reword : use commint, but edit the commit message
    • e, edit : use commit, but stop for amending
    • s, squash : use commit, but meld inro previous commit