It gives a warning not an error. It still compiles and runs.
When I compile it using Microsoft C++ it compiles with no warnings:
D:\rhs\c>cl test.cpp /EHsc
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30136 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
Microsoft (R) Incremental Linker Version 14.29.30136.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:test.exe
test.obj
Is it alright to manipulate strings in C++ this way:
string s = "Sting";
s[2] = 'a';
It works alright (and prints 'Sting'), but is it safe to do so?
If yes, does this mean they are mutable?
#include <iostream>
using namespace std;
int main()
{
string s = "Hello";
s[1] = 'a';
cout << s << endl;
char* t = "Hello";
t[1] = 'a'; // This line causes a crash
cout << t << endl;
return 0;
}
And it compiles, but the line:
t[1] = 'a'; // This line causes a crash
causes the program to crash.
I think the point is that strings are mutable, but string literals are not.
@BannedUser If we take a similar differential element to the opposite(bottom) of the element in the picture...then the vertical components will cancel out as they exert equal forces at P. So we take the resultant force only...
@BannedUser The derivation will be much simpler if you take rings as differential elements...
#include<iostream> #include<vector> using namespace std; int main() { int row; cout << "Enter the number of rows : "; cin >> row; int col; cout << "Enter the number of columns : "; cin >> col; vector<vector<int>> vec; //Taking user input for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { int x; cin >> x; vec[i].push_back(x); } } //Printing output for (auto vctr : vec) { for (auto ele : vctr) { cout << ele << " "; } cout << endl; } system("pause>0"); }
#include<iostream>
#include<vector>
using namespace std;
int main() {
int row;
cout << "Enter the number of rows : ";
cin >> row;
int col;
cout << "Enter the number of columns : ";
cin >> col;
vector<vector<int>> vec;
//Taking user input
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
int x;
cin >> x;
vec[i].push_back(x);
}
}
//Printing output
for (auto vctr : vec) {
for (auto ele : vctr) {
cout << ele << " ";
}
cout << endl;
}
system("pause>0");
#include<iostream>
#include<vector>
using namespace std;
int main() {
int row;
cout << "Enter the number of rows : ";
cin >> row;
int col;
cout << "Enter the number of columns : ";
cin >> col;
cout << "Now enter all the data" << endl;
// Declare a vector of vector<int> to hold all the data
vector<vector<int>> vec;
//Taking user input
for (int i = 0; i < row; i++) {
// We'll add the data for the current row to this vector<int>
vector<int> v_int;